HarshitJoshi commited on
Commit
0d64151
·
verified ·
1 Parent(s): 3c5007c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -27
app.py CHANGED
@@ -21,6 +21,17 @@ for i, url in enumerate(file_urls):
21
  else:
22
  download_file(file_urls[i], f"image_{i}.jpg")
23
 
 
 
 
 
 
 
 
 
 
 
 
24
  model = YOLO('modelbest.pt')
25
  path = [['image_0.jpg'], ['image_1.jpg']]
26
  video_path = [['video.mp4']]
@@ -31,30 +42,27 @@ def show_preds_image(image_path):
31
  results = outputs[0].cpu().numpy()
32
 
33
  for i, det in enumerate(results.boxes.xyxy):
34
- # Draw the bounding box
35
- cv2.rectangle(
36
- image,
37
- (int(det[0]), int(det[1])),
38
- (int(det[2]), int(det[3])),
39
- color=(0, 0, 255),
40
- thickness=2,
41
- lineType=cv2.LINE_AA
42
- )
43
-
44
- # Get the class label and confidence score
45
  class_id = int(results.boxes.cls[i])
46
- confidence = results.boxes.conf[i]
47
- label = f"{model.names[class_id]}: {confidence:.2f}"
 
 
 
 
 
 
48
 
49
- # Draw the label
50
- label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
51
- top_left = (int(det[0]), int(det[1]) - 10)
52
- bottom_right = (int(det[0]) + label_size[0], int(det[1]))
53
- cv2.rectangle(image, top_left, bottom_right, (0, 0, 255), cv2.FILLED)
54
- cv2.putText(image, label, (int(det[0]), int(det[1]) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
 
55
 
56
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
57
 
 
58
  # def show_preds_image(image_path):
59
  # image = cv2.imread(image_path)
60
  # outputs = model.predict(source=image_path)
@@ -94,17 +102,29 @@ def show_preds_video(video_path):
94
  frame_copy = frame.copy()
95
  outputs = model.predict(source=frame)
96
  results = outputs[0].cpu().numpy()
 
97
  for i, det in enumerate(results.boxes.xyxy):
98
- cv2.rectangle(
99
- frame_copy,
100
- (int(det[0]), int(det[1])),
101
- (int(det[2]), int(det[3])),
102
- color=(0, 0, 255),
103
- thickness=2,
104
- lineType=cv2.LINE_AA
105
- )
 
 
 
 
 
 
 
 
 
 
106
  yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
107
 
 
108
  inputs_video = [
109
  gr.Video(format="mp4", label="Input Video"),
110
  ]
 
21
  else:
22
  download_file(file_urls[i], f"image_{i}.jpg")
23
 
24
+ colors = {
25
+ 0: (255, 0, 0), # Red for class 0
26
+ 1: (0, 255, 0), # Green for class 1
27
+ 2: (0, 0, 255), # Blue for class 2
28
+ 3: (255, 255, 0), # Yellow for class 3
29
+ 4: (255, 0, 255), # Magenta for class 4
30
+ 5: (0, 255, 255), # Cyan for class 5
31
+ 6: (128, 0, 0), # Maroon for class 6
32
+ 7: (0, 128, 0), # Green (dark) for class 7
33
+ }
34
+
35
  model = YOLO('modelbest.pt')
36
  path = [['image_0.jpg'], ['image_1.jpg']]
37
  video_path = [['video.mp4']]
 
42
  results = outputs[0].cpu().numpy()
43
 
44
  for i, det in enumerate(results.boxes.xyxy):
 
 
 
 
 
 
 
 
 
 
 
45
  class_id = int(results.boxes.cls[i])
46
+ label = model.names[class_id]
47
+
48
+ # Get the bounding box coordinates
49
+ x1, y1, x2, y2 = int(det[0]), int(det[1]), int(det[2]), int(det[3])
50
+
51
+ # Draw the bounding box with the specified color
52
+ color = colors.get(class_id, (0, 0, 255))
53
+ cv2.rectangle(image, (x1, y1), (x2, y2), color, 2, cv2.LINE_AA)
54
 
55
+ # Calculate text size and position
56
+ label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 2)
57
+ text_x = x1 + (x2 - x1) // 2 - label_size[0] // 2
58
+ text_y = y1 + (y2 - y1) // 2 + label_size[1] // 2
59
+
60
+ # Draw the label text
61
+ cv2.putText(image, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2, cv2.LINE_AA)
62
 
63
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
64
 
65
+
66
  # def show_preds_image(image_path):
67
  # image = cv2.imread(image_path)
68
  # outputs = model.predict(source=image_path)
 
102
  frame_copy = frame.copy()
103
  outputs = model.predict(source=frame)
104
  results = outputs[0].cpu().numpy()
105
+
106
  for i, det in enumerate(results.boxes.xyxy):
107
+ class_id = int(results.boxes.cls[i])
108
+ label = model.names[class_id]
109
+
110
+ # Get the bounding box coordinates
111
+ x1, y1, x2, y2 = int(det[0]), int(det[1]), int(det[2]), int(det[3])
112
+
113
+ # Draw the bounding box with the specified color
114
+ color = colors.get(class_id, (0, 0, 255))
115
+ cv2.rectangle(frame_copy, (x1, y1), (x2, y2), color, 2, cv2.LINE_AA)
116
+
117
+ # Calculate text size and position
118
+ label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 2)
119
+ text_x = x1 + (x2 - x1) // 2 - label_size[0] // 2
120
+ text_y = y1 + (y2 - y1) // 2 + label_size[1] // 2
121
+
122
+ # Draw the label text
123
+ cv2.putText(frame_copy, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2, cv2.LINE_AA)
124
+
125
  yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
126
 
127
+
128
  inputs_video = [
129
  gr.Video(format="mp4", label="Input Video"),
130
  ]