Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,8 +10,8 @@ config = DetrConfig.from_pretrained("facebook/detr-resnet-50")
|
|
| 10 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
|
| 11 |
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 12 |
|
| 13 |
-
# Initialize the pipeline
|
| 14 |
-
od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor)
|
| 15 |
|
| 16 |
def draw_detections(image, detections):
|
| 17 |
# Convert PIL image to a numpy array
|
|
@@ -32,7 +32,8 @@ def draw_detections(image, detections):
|
|
| 32 |
# Draw rectangles and text with a larger font
|
| 33 |
cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
| 34 |
label_text = f'{label} {score:.2f}'
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
# Convert BGR to RGB for displaying
|
| 38 |
final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
|
|
@@ -40,10 +41,10 @@ def draw_detections(image, detections):
|
|
| 40 |
return final_pil_image
|
| 41 |
|
| 42 |
def get_pipeline_prediction(pil_image):
|
| 43 |
-
# Ensure the image is a PIL Image as expected by the model pipeline
|
| 44 |
-
if not isinstance(pil_image, Image.Image):
|
| 45 |
-
pil_image = Image.fromarray(pil_image.astype('uint8'), 'RGB')
|
| 46 |
try:
|
|
|
|
|
|
|
|
|
|
| 47 |
pipeline_output = od_pipe(pil_image)
|
| 48 |
processed_image = draw_detections(pil_image, pipeline_output)
|
| 49 |
return processed_image, pipeline_output
|
|
@@ -55,7 +56,7 @@ def get_pipeline_prediction(pil_image):
|
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
with gr.Row():
|
| 57 |
with gr.Column():
|
| 58 |
-
inp_image = gr.Image(label="Input image"
|
| 59 |
btn_run = gr.Button('Run Detection')
|
| 60 |
with gr.Column():
|
| 61 |
with gr.Tab("Annotated Image"):
|
|
|
|
| 10 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
|
| 11 |
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 12 |
|
| 13 |
+
# Initialize the pipeline, adjust the confidence_threshold if possible
|
| 14 |
+
od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor, confidence_threshold=0.5)
|
| 15 |
|
| 16 |
def draw_detections(image, detections):
|
| 17 |
# Convert PIL image to a numpy array
|
|
|
|
| 32 |
# Draw rectangles and text with a larger font
|
| 33 |
cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
| 34 |
label_text = f'{label} {score:.2f}'
|
| 35 |
+
# Increase the font size and text thickness
|
| 36 |
+
cv2.putText(np_image, label_text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 4)
|
| 37 |
|
| 38 |
# Convert BGR to RGB for displaying
|
| 39 |
final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
|
|
|
|
| 41 |
return final_pil_image
|
| 42 |
|
| 43 |
def get_pipeline_prediction(pil_image):
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
+
# Ensure PIL image is passed correctly
|
| 46 |
+
if isinstance(pil_image, np.ndarray):
|
| 47 |
+
pil_image = Image.fromarray(pil_image.astype('uint8'), 'RGB')
|
| 48 |
pipeline_output = od_pipe(pil_image)
|
| 49 |
processed_image = draw_detections(pil_image, pipeline_output)
|
| 50 |
return processed_image, pipeline_output
|
|
|
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
| 59 |
+
inp_image = gr.Image(label="Input image")
|
| 60 |
btn_run = gr.Button('Run Detection')
|
| 61 |
with gr.Column():
|
| 62 |
with gr.Tab("Annotated Image"):
|