Spaces:
Sleeping
Sleeping
update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465import cv2import numpy as npimport gradio as grimport matplotlib.pyplot as pltfrom transformers import pipelinemodel = pipeline("object-detection", "facebook/detr-resnet-50") #loading model#render functiondef render_results(raw_image, model_output):raw_image = np.array(raw_image)for detection in model_output:label = detection['label']score = detection['score']box = detection['box']xmin, ymin, xmax, ymax = box['xmin'], box['ymin'], box['xmax'], box['ymax']#Drawing the bounding boxcv2.rectangle(raw_image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)#Puting label and score near the bounding boxcv2.putText(raw_image, f"{label}: {score:.2f}", (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)return raw_imagedef get_object_counts(detections): ##to get count of object detected in the imageobject_counts = {}for detection in detections:label = detection['label']if label in object_counts:object_counts[label] += 1else:object_counts[label] = 1return object_countsdef generate_output_text(object_counts): ##to get the output stringoutput_text = "In this image there are"for label, count in object_counts.items():output_text += f" {count} {label},"output_text = output_text.rstrip(',') + "."return output_textdef main(pil_image):pipeline_output = model(pil_image) #model outputprocessed_image = render_results(pil_image, pipeline_output) ##process image by drawing bounding boxesoutput_text = generate_output_text(get_object_counts(pipeline_output)) ##output stringreturn processed_image, output_textdemo = gr.Interface(fn = main,inputs = gr.Image(label = "Input Image", type = "pil"),outputs = [gr.Image(label = "Modle output Predictions", type = "numpy"), gr.Text(label="Output Text")])demo.launch()
app.py+65