shamimjony1000 commited on
Commit
74c61e4
·
1 Parent(s): 6802d8e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+
5
+ examples=[["photo/a.png","Image1"],["photo/b.png","Image2"],
6
+ ["photo/c.png","Image3"],["photo/d.png","Image4"],
7
+ ["photo/e.png","Image5"],["photo/f.png","Image6"],
8
+ ["photo/g.png","Image7"],["photo/h.png","Image8"]]
9
+
10
+
11
+ def detect_objects_on_image(image_path):
12
+ image = cv2.imread(image_path)
13
+ model = YOLO("best.pt")
14
+ results = model.predict(image_path)
15
+ result = results[0]
16
+ output = []
17
+ for box in result.boxes:
18
+ x1, y1, x2, y2 = [
19
+ round(x) for x in box.xyxy[0].tolist()
20
+
21
+ ]
22
+ class_id = box.cls[0].item()
23
+ prob = round(box.conf[0].item(), 2)
24
+ output.append([
25
+ x1, y1, x2, y2, result.names[class_id], prob
26
+ ])
27
+ cv2.rectangle(
28
+ image,
29
+ (x1, y1),
30
+ (x2, y2),
31
+ color=(0, 0, 255),
32
+ thickness=2,
33
+ lineType=cv2.LINE_AA
34
+ )
35
+
36
+ cv2.putText(image,result.names[class_id], (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
37
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
38
+
39
+
40
+ inputs_image = [
41
+ gr.components.Image(type="filepath", label="Input Image"),
42
+ ]
43
+ outputs_image = [
44
+ gr.components.Image(type="numpy", label="Output Image"),
45
+ ]
46
+ demo = gr.Interface(
47
+ fn=detect_objects_on_image,
48
+ inputs=inputs_image,
49
+ outputs=outputs_image,
50
+ title="Biker Helmet and Number Plate Detection",
51
+ examples=examples,
52
+ cache_examples=False,
53
+ )
54
+
55
+ if __name__ == "__main__":
56
+ demo.launch()