eldhoskj commited on
Commit
8b8d36e
·
verified ·
1 Parent(s): 46921f5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import gradio as gr
4
+ import requests
5
+ from io import BytesIO
6
+
7
+ # Configuration files
8
+ config_file = "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
9
+ frozen_model = "frozen_inference_graph.pb"
10
+
11
+ # Load model and set it to use the GPU
12
+ model = cv2.dnn.DetectionModel(frozen_model, config_file)
13
+ model.setInputSize(320, 320)
14
+ model.setInputScale(1.0 / 127.5)
15
+ model.setInputMean((127.5, 127.5, 127.5))
16
+ model.setInputSwapRB(True)
17
+ model.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
18
+ model.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
19
+
20
+ # Load class labels
21
+ classLabels = []
22
+ with open('labels.txt', 'rt') as f:
23
+ classLabels = f.read().rstrip('\n').split('\n')
24
+
25
+ def detect_objects(frame):
26
+ """
27
+ Detect objects in a single frame and return their coordinates and names.
28
+ :param frame: Input image/frame
29
+ :return: List of detected objects with coordinates and names
30
+ """
31
+ detections = []
32
+
33
+ # Detect objects in the frame
34
+ ClassIndex, confidence, bbox = model.detect(frame, confThreshold=0.55)
35
+
36
+ if len(ClassIndex) != 0:
37
+ for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidence.flatten(), bbox):
38
+ if ClassInd <= 80:
39
+ x, y, w, h = boxes
40
+ detected_object = {
41
+ "name": classLabels[ClassInd - 1],
42
+ "coordinates": {
43
+ "x": int(x),
44
+ "y": int(y),
45
+ "width": int(w),
46
+ "height": int(h)
47
+ }
48
+ }
49
+ detections.append(detected_object)
50
+
51
+ # Draw bounding box and label on the frame
52
+ cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
53
+ cv2.putText(frame, classLabels[ClassInd - 1].upper(), (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
54
+
55
+ return frame, detections
56
+
57
+ def get_image_from_url(url):
58
+ response = requests.get(url)
59
+ image = np.asarray(bytearray(response.content), dtype="uint8")
60
+ image = cv2.imdecode(image, cv2.IMREAD_COLOR)
61
+ return image
62
+
63
+ def detect_objects_in_image_url(url):
64
+ frame = get_image_from_url(url)
65
+ result_frame, detected_objects = detect_objects(frame)
66
+ return result_frame, detected_objects
67
+
68
+ # Define the Gradio interface
69
+ iface = gr.Interface(
70
+ fn=detect_objects_in_image_url,
71
+ inputs="text",
72
+ outputs=[gr.Image(type="numpy"), gr.JSON()],
73
+ title="Object Detection",
74
+ description="Enter an image URL to detect objects. The bounding boxes and labels will be drawn on the image, and the detected objects will be returned as JSON."
75
+ )
76
+
77
+ # Launch the interface
78
+ iface.launch()