ethanrom commited on
Commit
358f1f1
·
1 Parent(s): 512d460

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -75
app.py CHANGED
@@ -1,84 +1,104 @@
1
  import gradio as gr
2
- import torch
3
- from sahi.prediction import ObjectPrediction
4
- from sahi.utils.cv import visualize_object_predictions, read_image
5
- from ultralyticsplus import YOLO
6
 
7
- # Images
8
- #torch.hub.download_url_to_file('https://ibb.co/zrFWRhJ][img]https://i.ibb.co/37v5Nyz/01-missing-hole-02-jpg-rf-3bd05d8784b12c66da03831d326edd2b.jpg', 'hole.jpg')
9
- #torch.hub.download_url_to_file('https://ibb.co/qgPMXvN][img]https://i.ibb.co/rp1GTVx/01-mouse-bite-04-jpg-rf-8b3b06d80ac553a92606bdac3e394bb9.jpg', 'mouse.jpg')
10
- #torch.hub.download_url_to_file('https://ibb.co/0Q35SLN][img]https://i.ibb.co/DLndyFX/12-spur-05-jpg-rf-39e4c0bbc17325d83c938d49e5efdf47.jpg', 'spur.jpg')
11
 
12
- def yolov8_inference(
13
- image: gr.inputs.Image = None,
14
- model_path: gr.inputs.Dropdown = None,
15
- image_size: gr.inputs.Slider = 640,
16
- conf_threshold: gr.inputs.Slider = 0.25,
17
- iou_threshold: gr.inputs.Slider = 0.45,
18
- ):
19
- """
20
- YOLOv8 inference function
21
- Args:
22
- image: Input image
23
- model_path: Path to the model
24
- image_size: Image size
25
- conf_threshold: Confidence threshold
26
- iou_threshold: IOU threshold
27
- Returns:
28
- Rendered image
29
- """
30
- model = YOLO(model_path)
31
- model.conf = conf_threshold
32
- model.iou = iou_threshold
33
- results = model.predict(image, imgsz=image_size, return_outputs=True)
34
- object_prediction_list = []
35
- for _, image_results in enumerate(results):
36
- if len(image_results)!=0:
37
- image_predictions_in_xyxy_format = image_results['det']
38
- for pred in image_predictions_in_xyxy_format:
39
- x1, y1, x2, y2 = (
40
- int(pred[0]),
41
- int(pred[1]),
42
- int(pred[2]),
43
- int(pred[3]),
44
- )
45
- bbox = [x1, y1, x2, y2]
46
- score = pred[4]
47
- category_name = model.model.names[int(pred[5])]
48
- category_id = pred[5]
49
- object_prediction = ObjectPrediction(
50
- bbox=bbox,
51
- category_id=int(category_id),
52
- score=score,
53
- category_name=category_name,
54
- )
55
- object_prediction_list.append(object_prediction)
56
 
57
- image = read_image(image)
58
- output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
59
- return output_image['image']
60
-
 
 
 
 
 
 
 
61
 
62
- inputs = [
63
- gr.inputs.Image(type="filepath", label="Input Image"),
64
- gr.inputs.Dropdown(["ethanrom/pcb_def"],
65
- default="ethanrom/pcb_def", label="Model"),
66
- gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
67
- gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
68
- gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- outputs = gr.outputs.Image(type="filepath", label="Output Image")
72
- title = "YOLOv8 PCB defect detection"
73
 
74
- #examples = [['highway.jpg', 'ethanrom/pcb_def', 640, 0.25, 0.45]]
75
- demo_app = gr.Interface(
76
- fn=yolov8_inference,
77
- inputs=inputs,
78
- outputs=outputs,
79
- title=title,
80
- #examples=examples,
81
- #cache_examples=True,
82
- theme='huggingface',
 
 
83
  )
84
- demo_app.launch(debug=True, enable_queue=True)
 
 
 
 
 
1
  import gradio as gr
2
+ import cv2
3
+ import requests
4
+ import os
 
5
 
6
+ from ultralytics import YOLO
 
 
 
7
 
8
+ file_urls = [
9
+ #'https://www.dropbox.com/s/b5g97xo901zb3ds/pothole_example.jpg?dl=1',
10
+ #'https://www.dropbox.com/s/86uxlxxlm1iaexa/pothole_screenshot.png?dl=1',
11
+ 'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
12
+ ]
13
+
14
+ def download_file(url, save_name):
15
+ url = url
16
+ if not os.path.exists(save_name):
17
+ file = requests.get(url)
18
+ open(save_name, 'wb').write(file.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ for i, url in enumerate(file_urls):
21
+ if 'mp4' in file_urls[i]:
22
+ download_file(
23
+ file_urls[i],
24
+ f"video.mp4"
25
+ )
26
+ else:
27
+ download_file(
28
+ file_urls[i],
29
+ f"image_{i}.jpg"
30
+ )
31
 
32
+ model = YOLO('yolov8_pcb.pt')
33
+ path = [['spur.jpg'], ['mouse.jpg']]
34
+ video_path = [['video.mp4']]
35
+
36
+ def show_preds_image(image_path):
37
+ image = cv2.imread(image_path)
38
+ outputs = model.predict(source=image_path)
39
+ results = outputs[0].cpu().numpy()
40
+ for i, det in enumerate(results.boxes.xyxy):
41
+ cv2.rectangle(
42
+ image,
43
+ (int(det[0]), int(det[1])),
44
+ (int(det[2]), int(det[3])),
45
+ color=(0, 0, 255),
46
+ thickness=2,
47
+ lineType=cv2.LINE_AA
48
+ )
49
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
50
+
51
+ inputs_image = [
52
+ gr.components.Image(type="filepath", label="Input Image"),
53
+ ]
54
+ outputs_image = [
55
+ gr.components.Image(type="numpy", label="Output Image"),
56
  ]
57
+ interface_image = gr.Interface(
58
+ fn=show_preds_image,
59
+ inputs=inputs_image,
60
+ outputs=outputs_image,
61
+ title="PCB Defect Detector",
62
+ examples=path,
63
+ cache_examples=False,
64
+ )
65
+
66
+ def show_preds_video(video_path):
67
+ cap = cv2.VideoCapture(video_path)
68
+ while(cap.isOpened()):
69
+ ret, frame = cap.read()
70
+ if ret:
71
+ frame_copy = frame.copy()
72
+ outputs = model.predict(source=frame)
73
+ results = outputs[0].cpu().numpy()
74
+ for i, det in enumerate(results.boxes.xyxy):
75
+ cv2.rectangle(
76
+ frame_copy,
77
+ (int(det[0]), int(det[1])),
78
+ (int(det[2]), int(det[3])),
79
+ color=(0, 0, 255),
80
+ thickness=2,
81
+ lineType=cv2.LINE_AA
82
+ )
83
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
84
 
85
+ inputs_video = [
86
+ gr.components.Video(type="filepath", label="Input Video"),
87
 
88
+ ]
89
+ outputs_video = [
90
+ gr.components.Image(type="numpy", label="Output Image"),
91
+ ]
92
+ interface_video = gr.Interface(
93
+ fn=show_preds_video,
94
+ inputs=inputs_video,
95
+ outputs=outputs_video,
96
+ title="PCB Defect Detector",
97
+ examples=video_path,
98
+ cache_examples=False,
99
  )
100
+
101
+ gr.TabbedInterface(
102
+ [interface_image, interface_video],
103
+ tab_names=['Image inference', 'Video inference']
104
+ ).queue().launch()