qbcode commited on
Commit
4685ada
·
verified ·
1 Parent(s): b56eaed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -39
app.py CHANGED
@@ -2,30 +2,15 @@ import gradio as gr
2
  import cv2
3
  import numpy as np
4
 
5
- # Define function to detect objects using YOLO
6
- def detect_objects(image, model_type):
7
- # Load the YOLO model based on the model type
8
- if model_type == 'YOLOv7':
9
- weights_path = "path_to_yolov7_weights"
10
- config_path = "path_to_yolov7_config"
11
- net = cv2.dnn.readNet(weights_path, config_path)
12
- layer_names = net.getLayerNames()
13
- output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
14
- elif model_type == 'YOLOv8':
15
- weights_path = "path_to_yolov8_weights"
16
- config_path = "path_to_yolov8_config"
17
- net = cv2.dnn.readNet(weights_path, config_path)
18
- layer_names = net.getLayerNames()
19
- output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
20
- elif model_type == 'YOLO-NAS':
21
- weights_path = "path_to_yolo_nas_weights"
22
- config_path = "path_to_yolo_nas_config"
23
- net = cv2.dnn.readNet(weights_path, config_path)
24
- layer_names = net.getLayerNames()
25
- output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
26
- else:
27
- return "Invalid Model Type"
28
-
29
  # Detect objects in the image
30
  blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
31
  net.setInput(blob)
@@ -54,7 +39,10 @@ def detect_objects(image, model_type):
54
 
55
  # Non-max suppression to remove overlapping boxes
56
  indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
57
-
 
 
 
58
  # Draw bounding boxes on the image
59
  for i in range(len(boxes)):
60
  if i in indexes:
@@ -64,20 +52,10 @@ def detect_objects(image, model_type):
64
  cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
65
  cv2.putText(image, label, (x, y + 30), cv2.FONT_HERSHEY_PLAIN, 3, color, 3)
66
 
67
- return image
68
 
69
  # Create Gradio interface
70
- models = ["YOLOv7", "YOLOv8", "YOLO-NAS"]
71
-
72
- pages = []
73
- for model in models:
74
- inputs = [
75
- gr.inputs.Image(label="Upload Image or Video"),
76
- ]
77
-
78
- outputs = gr.outputs.Image(label=f"{model} Output")
79
-
80
- page = gr.Interface(detect_objects, inputs, outputs, title=f"{model} Object Detection", description=f"Identify objects in images or videos using {model}").launch()
81
- pages.append((f"{model} Model", page))
82
 
83
- gr.Interface(pages, gr.Interface.MULTIPAGE).launch()
 
2
  import cv2
3
  import numpy as np
4
 
5
+ # Load YOLOv7 model
6
+ weights_path = "cattle.pt"
7
+ config_path = "yolov7.yaml"
8
+ net = cv2.dnn.readNet(weights_path, config_path)
9
+ layer_names = net.getLayerNames()
10
+ output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
11
+
12
+ # Define function to detect objects using YOLOv7
13
+ def detect_objects(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # Detect objects in the image
15
  blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
16
  net.setInput(blob)
 
39
 
40
  # Non-max suppression to remove overlapping boxes
41
  indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
42
+
43
+ # Count detected objects
44
+ object_count = len(indexes)
45
+
46
  # Draw bounding boxes on the image
47
  for i in range(len(boxes)):
48
  if i in indexes:
 
52
  cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
53
  cv2.putText(image, label, (x, y + 30), cv2.FONT_HERSHEY_PLAIN, 3, color, 3)
54
 
55
+ return image, object_count
56
 
57
  # Create Gradio interface
58
+ inputs = gr.inputs.Image(label="Upload Image or Video")
59
+ outputs = [gr.outputs.Image(label="Output Image with Objects Detected"), gr.outputs.Text(label="Object Count")]
 
 
 
 
 
 
 
 
 
 
60
 
61
+ gr.Interface(detect_objects, inputs, outputs, title="YOLOv7 Object Detection").launch()