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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -45
app.py CHANGED
@@ -1,56 +1,34 @@
1
- import gradio as gr
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)
17
- outs = net.forward(output_layers)
18
-
19
- # Process detection results
20
- class_ids = []
21
- confidences = []
22
- boxes = []
23
- for out in outs:
24
- for detection in out:
25
- scores = detection[5:]
26
- class_id = np.argmax(scores)
27
- confidence = scores[class_id]
28
- if confidence > 0.5:
29
- # Object detected
30
- center_x = int(detection[0] * image.shape[1])
31
- center_y = int(detection[1] * image.shape[0])
32
- w = int(detection[2] * image.shape[1])
33
- h = int(detection[3] * image.shape[0])
34
- x = int(center_x - w / 2)
35
- y = int(center_y - h / 2)
36
- boxes.append([x, y, w, h])
37
- confidences.append(float(confidence))
38
- class_ids.append(class_id)
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:
49
- x, y, w, h = boxes[i]
50
- label = str(class_ids[i])
51
- color = (255, 0, 0) # BGR color format
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
 
 
1
+ import torch
2
+ from torchvision import transforms
3
  import numpy as np
4
 
5
  # Load YOLOv7 model
6
  weights_path = "cattle.pt"
7
  config_path = "yolov7.yaml"
8
+ net = None # Initialize your YOLOv7 model here
 
 
9
 
10
  # Define function to detect objects using YOLOv7
11
  def detect_objects(image):
12
+ global net
13
+
14
+ if net is None:
15
+ # Initialize your YOLOv7 model if not already initialized
16
+ net = ...
17
+
18
+ # Perform any necessary preprocessing on the image
19
+ transform = transforms.Compose([
20
+ transforms.Resize((416, 416)), # Resize image to expected input size
21
+ transforms.ToTensor(), # Convert image to tensor
22
+ ])
23
+ image_tensor = transform(image).unsqueeze(0)
24
+
25
+ # Perform object detection
26
+ with torch.no_grad():
27
+ # Forward pass through the network
28
+ outs = net(image_tensor)
29
+
30
+ # Process detection results and draw bounding boxes (similar to your previous code)
31
+ ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  return image, object_count
34