Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,13 +24,8 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Use GPU
|
|
| 24 |
model = attempt_load(model_path, device=device) # Placeholder for model loading
|
| 25 |
model.eval() # Set the model to evaluation mode
|
| 26 |
|
| 27 |
-
#def preprocess_image(image_path):
|
| 28 |
def preprocess_image(image):
|
| 29 |
-
#img0 = cv2.imread(image_path)
|
| 30 |
-
print("in preprocess-0 image.shape:",image.size)
|
| 31 |
img = letterbox(image, 640, stride=32, auto=True)[0] # Resize and pad to 640x640
|
| 32 |
-
#img = letterbox(img0, 640, stride=32, auto=True)[0] # Resize and pad to 640x640
|
| 33 |
-
print("in preprocess-1 img.shape:",img.shape)
|
| 34 |
img = img.transpose(2, 0, 1)[::-1] # Convert BGR to RGB,
|
| 35 |
img = np.ascontiguousarray(img)
|
| 36 |
img = torch.from_numpy(img).to(device)
|
|
@@ -38,10 +33,8 @@ def preprocess_image(image):
|
|
| 38 |
img /= 255.0 # 0 - 255 to 0.0 - 1.0
|
| 39 |
if img.ndimension() == 3:
|
| 40 |
img = img.unsqueeze(0)
|
| 41 |
-
print("in preprocess-2 img.shape:",img.shape)
|
| 42 |
|
| 43 |
return img, image
|
| 44 |
-
#return img, img0
|
| 45 |
|
| 46 |
def infer(model, img):
|
| 47 |
with torch.no_grad():
|
|
@@ -62,13 +55,11 @@ def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
|
|
| 62 |
coords[:, :4].clip_(min=0, max=img1_shape[0]) # clip boxes
|
| 63 |
return coords
|
| 64 |
|
| 65 |
-
#def postprocess(pred, img0_shape, img):
|
| 66 |
def postprocess(pred, img0, img):
|
| 67 |
pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False)
|
| 68 |
results = []
|
| 69 |
for det in pred: # detections per image
|
| 70 |
if len(det):
|
| 71 |
-
#det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0_shape).round()
|
| 72 |
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
|
| 73 |
for *xyxy, conf, cls in reversed(det):
|
| 74 |
results.append((xyxy, conf, cls))
|
|
@@ -76,16 +67,15 @@ def postprocess(pred, img0, img):
|
|
| 76 |
|
| 77 |
def detect_objects(image_path):
|
| 78 |
dicom_image, dicom_meta = read_and_preprocess_dicom(image_path)
|
| 79 |
-
#img, img0 = preprocess_image(image_path)
|
| 80 |
img, img0 = preprocess_image(dicom_image)
|
| 81 |
pred = infer(model, img)
|
| 82 |
-
#results = postprocess(pred, img0.shape, img)
|
| 83 |
results = postprocess(pred, dicom_image, img)
|
| 84 |
return results, dicom_image
|
| 85 |
|
| 86 |
def draw_bounding_boxes(img, results):
|
| 87 |
for (x1, y1, x2, y2), conf, cls in results:
|
| 88 |
x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
|
|
|
|
| 89 |
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 90 |
cv2.putText(img, f'{model.names[int(cls)]} {conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
|
| 91 |
return img
|
|
@@ -94,7 +84,6 @@ def show_preds_image(filepath):
|
|
| 94 |
results, img0 = detect_objects(filepath)
|
| 95 |
print("in show preds:",results)
|
| 96 |
#results = detect_objects(filepath)
|
| 97 |
-
#img0 = cv2.imread(filepath)
|
| 98 |
img_with_boxes = draw_bounding_boxes(img0, results)
|
| 99 |
return cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 100 |
|
|
|
|
| 24 |
model = attempt_load(model_path, device=device) # Placeholder for model loading
|
| 25 |
model.eval() # Set the model to evaluation mode
|
| 26 |
|
|
|
|
| 27 |
def preprocess_image(image):
|
|
|
|
|
|
|
| 28 |
img = letterbox(image, 640, stride=32, auto=True)[0] # Resize and pad to 640x640
|
|
|
|
|
|
|
| 29 |
img = img.transpose(2, 0, 1)[::-1] # Convert BGR to RGB,
|
| 30 |
img = np.ascontiguousarray(img)
|
| 31 |
img = torch.from_numpy(img).to(device)
|
|
|
|
| 33 |
img /= 255.0 # 0 - 255 to 0.0 - 1.0
|
| 34 |
if img.ndimension() == 3:
|
| 35 |
img = img.unsqueeze(0)
|
|
|
|
| 36 |
|
| 37 |
return img, image
|
|
|
|
| 38 |
|
| 39 |
def infer(model, img):
|
| 40 |
with torch.no_grad():
|
|
|
|
| 55 |
coords[:, :4].clip_(min=0, max=img1_shape[0]) # clip boxes
|
| 56 |
return coords
|
| 57 |
|
|
|
|
| 58 |
def postprocess(pred, img0, img):
|
| 59 |
pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False)
|
| 60 |
results = []
|
| 61 |
for det in pred: # detections per image
|
| 62 |
if len(det):
|
|
|
|
| 63 |
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
|
| 64 |
for *xyxy, conf, cls in reversed(det):
|
| 65 |
results.append((xyxy, conf, cls))
|
|
|
|
| 67 |
|
| 68 |
def detect_objects(image_path):
|
| 69 |
dicom_image, dicom_meta = read_and_preprocess_dicom(image_path)
|
|
|
|
| 70 |
img, img0 = preprocess_image(dicom_image)
|
| 71 |
pred = infer(model, img)
|
|
|
|
| 72 |
results = postprocess(pred, dicom_image, img)
|
| 73 |
return results, dicom_image
|
| 74 |
|
| 75 |
def draw_bounding_boxes(img, results):
|
| 76 |
for (x1, y1, x2, y2), conf, cls in results:
|
| 77 |
x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
|
| 78 |
+
print("results in draw_bounding box:",[(x1, y1, x2, y2), conf, cls])
|
| 79 |
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 80 |
cv2.putText(img, f'{model.names[int(cls)]} {conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
|
| 81 |
return img
|
|
|
|
| 84 |
results, img0 = detect_objects(filepath)
|
| 85 |
print("in show preds:",results)
|
| 86 |
#results = detect_objects(filepath)
|
|
|
|
| 87 |
img_with_boxes = draw_bounding_boxes(img0, results)
|
| 88 |
return cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 89 |
|