Spaces:
Sleeping
Sleeping
Update app_v2.py
Browse files
app_v2.py
CHANGED
|
@@ -13,15 +13,19 @@ def draw_detections(image, detections):
|
|
| 13 |
np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
|
| 14 |
|
| 15 |
for detection in detections:
|
| 16 |
-
# Extract scores, labels, and bounding boxes
|
| 17 |
score = detection['score']
|
| 18 |
label = detection['label']
|
| 19 |
-
box = detection['box']
|
| 20 |
-
x_min
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
| 23 |
cv2.putText(np_image, f'{label} {score:.2f}', (x_min, max(y_min - 10, 0)),
|
| 24 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (
|
| 25 |
|
| 26 |
# Convert BGR to RGB for displaying
|
| 27 |
final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
|
|
@@ -29,48 +33,6 @@ def draw_detections(image, detections):
|
|
| 29 |
final_pil_image = Image.fromarray(final_image)
|
| 30 |
return final_pil_image
|
| 31 |
|
| 32 |
-
# Initialize objects from transformers
|
| 33 |
-
config = DetrConfig.from_pretrained("facebook/detr-resnet-50")
|
| 34 |
-
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
|
| 35 |
-
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 36 |
-
od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor)
|
| 37 |
-
|
| 38 |
-
def get_pipeline_prediction(pil_image):
|
| 39 |
-
try:
|
| 40 |
-
# Run the object detection pipeline
|
| 41 |
-
pipeline_output = od_pipe(pil_image)
|
| 42 |
-
|
| 43 |
-
# Draw the detection results on the image
|
| 44 |
-
processed_image = draw_detections(pil_image, pipeline_output)
|
| 45 |
-
|
| 46 |
-
# Provide both the image and the JSON detection results
|
| 47 |
-
return processed_image, pipeline_output
|
| 48 |
-
except Exception as e:
|
| 49 |
-
# Log the error
|
| 50 |
-
print(f"An error occurred: {str(e)}")
|
| 51 |
-
# Return a message and an empty JSON
|
| 52 |
-
return pil_image, {"error": str(e)}
|
| 53 |
-
|
| 54 |
-
##def get_pipeline_prediction(pil_image):
|
| 55 |
-
## try:
|
| 56 |
-
# Run the object detection pipeline
|
| 57 |
-
## pipeline_output = od_pipe(pil_image)
|
| 58 |
-
|
| 59 |
-
# Debugging: print the keys in the output dictionary
|
| 60 |
-
## if pipeline_output:
|
| 61 |
-
## print("Keys available in the detection output:", pipeline_output[0].keys())
|
| 62 |
-
|
| 63 |
-
# Draw the detection results on the image
|
| 64 |
-
## processed_image = draw_detections(pil_image, pipeline_output)
|
| 65 |
-
|
| 66 |
-
# Provide both the image and the JSON detection results
|
| 67 |
-
## return processed_image, pipeline_output
|
| 68 |
-
## except Exception as e:
|
| 69 |
-
# Log the error
|
| 70 |
-
#3 print(f"An error occurred: {str(e)}")
|
| 71 |
-
# Return a message and an empty JSON
|
| 72 |
-
## return pil_image, {"error": str(e)}
|
| 73 |
-
|
| 74 |
demo = gr.Interface(
|
| 75 |
fn=get_pipeline_prediction,
|
| 76 |
inputs=gr.Image(label="Input image", type="pil"),
|
|
@@ -80,4 +42,4 @@ demo = gr.Interface(
|
|
| 80 |
]
|
| 81 |
)
|
| 82 |
|
| 83 |
-
demo.launch()
|
|
|
|
| 13 |
np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
|
| 14 |
|
| 15 |
for detection in detections:
|
| 16 |
+
# Extract scores, labels, and bounding boxes correctly
|
| 17 |
score = detection['score']
|
| 18 |
label = detection['label']
|
| 19 |
+
box = detection['box']
|
| 20 |
+
x_min = box['xmin']
|
| 21 |
+
y_min = box['ymin']
|
| 22 |
+
x_max = box['xmax']
|
| 23 |
+
y_max = box['ymax']
|
| 24 |
+
|
| 25 |
+
# Draw rectangles and text on the image
|
| 26 |
cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
| 27 |
cv2.putText(np_image, f'{label} {score:.2f}', (x_min, max(y_min - 10, 0)),
|
| 28 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
|
| 29 |
|
| 30 |
# Convert BGR to RGB for displaying
|
| 31 |
final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
|
|
|
|
| 33 |
final_pil_image = Image.fromarray(final_image)
|
| 34 |
return final_pil_image
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=get_pipeline_prediction,
|
| 38 |
inputs=gr.Image(label="Input image", type="pil"),
|
|
|
|
| 42 |
]
|
| 43 |
)
|
| 44 |
|
| 45 |
+
demo.launch()
|