qbcode commited on
Commit
88439ab
·
verified ·
1 Parent(s): f5e8009

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -39
app.py CHANGED
@@ -1,40 +1,52 @@
1
- import gradio as gr
2
- import torch
3
- from torchvision import transforms
4
- import numpy as np
5
-
6
- # Load YOLOv7 model
7
- weights_path = "cattle.pt"
8
- config_path = "yolov7.yaml"
9
- net = None # Initialize your YOLOv7 model here
10
-
11
- # Define function to detect objects using YOLOv7
12
- def detect_objects(image):
13
- global net
14
-
15
- if net is None:
16
- # Initialize your YOLOv7 model if not already initialized
17
- net = ...
18
-
19
- # Perform any necessary preprocessing on the image
20
- transform = transforms.Compose([
21
- transforms.Resize((416, 416)), # Resize image to expected input size
22
- transforms.ToTensor(), # Convert image to tensor
23
- ])
24
- image_tensor = transform(image).unsqueeze(0)
25
-
26
- # Perform object detection
27
- with torch.no_grad():
28
- # Forward pass through the network
29
- outs = net(image_tensor)
30
-
31
- # Process detection results and draw bounding boxes (similar to your previous code)
32
- ...
33
-
34
- return image, object_count
35
-
36
- # Create Gradio interface
37
- inputs = gr.inputs.Image(label="Upload Image or Video")
38
- outputs = [gr.outputs.Image(label="Output Image with Objects Detected"), gr.outputs.Text(label="Object Count")]
39
-
 
 
 
 
 
 
 
 
 
 
 
 
40
  gr.Interface(detect_objects, inputs, outputs, title="YOLOv7 Object Detection").launch()
 
1
+ import gradio as gr
2
+ import torch
3
+ from torchvision import transforms
4
+ from models.experimental import attempt_load
5
+ from utils.general import non_max_suppression, plot_one_box
6
+ import numpy as np
7
+
8
+ # Load YOLOv7 model
9
+ weights_path = "cattle.pt"
10
+ config_path = "yolov7.yaml"
11
+
12
+ # Initialize your YOLOv7 model here
13
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
14
+ net = attempt_load(weights_path, map_location=device)
15
+
16
+ # Define function to detect objects using YOLOv7
17
+ def detect_objects(image):
18
+ global net
19
+
20
+ # Perform any necessary preprocessing on the image
21
+ transform = transforms.Compose([
22
+ transforms.Resize((416, 416)), # Resize image to expected input size
23
+ transforms.ToTensor(), # Convert image to tensor
24
+ ])
25
+ image_tensor = transform(image).unsqueeze(0)
26
+
27
+ # Perform object detection
28
+ with torch.no_grad():
29
+ # Forward pass through the network
30
+ outs = net(image_tensor)
31
+
32
+ # Apply non-maximum suppression
33
+ pred = non_max_suppression(outs, conf_thres=0.4, iou_thres=0.5, classes=None, agnostic=False)
34
+
35
+ # Process detection results and draw bounding boxes
36
+ object_count = 0
37
+ for i, det in enumerate(pred):
38
+ # Skip if no detections
39
+ if len(det):
40
+ # Increment object count
41
+ object_count += len(det)
42
+ # Loop over the detections
43
+ for *xyxy, conf, cls in reversed(det):
44
+ # Draw bounding box
45
+ plot_one_box(xyxy, image, label=f'{conf:.2f}', color=(255, 0, 0), line_thickness=3)
46
+
47
+ return image, object_count
48
+
49
+ # Create Gradio interface
50
+ inputs = gr.inputs.Image(label="Upload Image or Video")
51
+ outputs = [gr.outputs.Image(label="Output Image with Objects Detected"), gr.outputs.Text(label="Object Count")]
52
  gr.Interface(detect_objects, inputs, outputs, title="YOLOv7 Object Detection").launch()