Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
import matplotlib.pyplot as plt
|
3 |
from PIL import Image
|
4 |
-
from ultralyticsplus import YOLO
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
from transformers import pipeline
|
8 |
import requests
|
9 |
from io import BytesIO
|
|
|
10 |
|
11 |
model = YOLO('best (1).pt')
|
12 |
-
model2 = pipeline('image-classification','Kaludi/csgo-weapon-classification')
|
13 |
name = ['grenade','knife','pistol','rifle']
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
# for i, r in enumerate(results):
|
35 |
|
36 |
# # Plot results image
|
@@ -78,26 +80,27 @@ def response(image):
|
|
78 |
def response2(image: gr.Image = None,image_size: gr.Slider = 640, conf_threshold: gr.Slider = 0.3, iou_threshold: gr.Slider = 0.6):
|
79 |
|
80 |
results = model.predict(image, conf=conf_threshold, iou=iou_threshold, imgsz=image_size)
|
81 |
-
|
82 |
box = results[0].boxes
|
83 |
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
|
|
|
87 |
weapon_name, text_detection = response(image)
|
88 |
|
89 |
|
90 |
# xywh = int(results.boxes.xywh)
|
91 |
# x = xywh[0]
|
92 |
# y = xywh[1]
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
return render, text_detection, weapon_name
|
97 |
|
98 |
|
99 |
inputs = [
|
100 |
-
gr.Image(type="
|
101 |
gr.Slider(minimum=320, maximum=1280, value=640,
|
102 |
step=32, label="Image Size"),
|
103 |
gr.Slider(minimum=0.0, maximum=1.0, value=0.3,
|
@@ -106,23 +109,59 @@ inputs = [
|
|
106 |
step=0.05, label="IOU Threshold"),
|
107 |
]
|
108 |
|
109 |
-
|
110 |
-
outputs = [gr.Image( type="filepath", label="Output Image"),
|
111 |
gr.Textbox(label="Result"),
|
112 |
gr.Textbox(label="Weapon Name")
|
113 |
]
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
[
|
118 |
-
[
|
119 |
-
[im4, 640, 0.15, 0.6]
|
120 |
]
|
121 |
-
|
122 |
-
|
123 |
title = 'Weapon Detection Finetuned YOLOv8'
|
124 |
-
|
125 |
description = 'Image Size: Defines the image size for inference.\nConfidence Treshold: Sets the minimum confidence threshold for detections.\nIOU Treshold: Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Useful for reducing duplicates.'
|
126 |
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import matplotlib.pyplot as plt
|
3 |
from PIL import Image
|
4 |
+
from ultralyticsplus import YOLO
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
from transformers import pipeline
|
8 |
import requests
|
9 |
from io import BytesIO
|
10 |
+
import os
|
11 |
|
12 |
model = YOLO('best (1).pt')
|
|
|
13 |
name = ['grenade','knife','pistol','rifle']
|
14 |
+
image_directory = "/home/user/app/image"
|
15 |
+
video_directory = "/home/user/app/video"
|
16 |
+
|
17 |
+
# url_example="https://drive.google.com/file/d/1bBq0bNmJ5X83tDWCzdzHSYCdg-aUL4xO/view?usp=drive_link"
|
18 |
+
# url_example='https://drive.google.com/uc?id=' + url_example.split('/')[-2]
|
19 |
+
# r = requests.get(url_example)
|
20 |
+
# im1 = Image.open(BytesIO(r.content))
|
21 |
+
|
22 |
+
# url_example="https://drive.google.com/file/d/16Z7QzvZ99fbEPj1sls_jOCJBsC0h_dYZ/view?usp=drive_link"
|
23 |
+
# url_example='https://drive.google.com/uc?id=' + url_example.split('/')[-2]
|
24 |
+
# r = requests.get(url_example)
|
25 |
+
# im2 = Image.open(BytesIO(r.content))
|
26 |
+
|
27 |
+
# url_example="https://drive.google.com/file/d/13mjTMS3eR0AKYSbV-Fpb3fTBno_T42JN/view?usp=drive_link"
|
28 |
+
# url_example='https://drive.google.com/uc?id=' + url_example.split('/')[-2]
|
29 |
+
# r = requests.get(url_example)
|
30 |
+
# im3 = Image.open(BytesIO(r.content))
|
31 |
+
|
32 |
+
# url_example="https://drive.google.com/file/d/1-XpFsa_nz506Ul6grKElVJDu_Jl3KZIF/view?usp=drive_link"
|
33 |
+
# url_example='https://drive.google.com/uc?id=' + url_example.split('/')[-2]
|
34 |
+
# r = requests.get(url_example)
|
35 |
+
# im4 = Image.open(BytesIO(r.content))
|
36 |
# for i, r in enumerate(results):
|
37 |
|
38 |
# # Plot results image
|
|
|
80 |
def response2(image: gr.Image = None,image_size: gr.Slider = 640, conf_threshold: gr.Slider = 0.3, iou_threshold: gr.Slider = 0.6):
|
81 |
|
82 |
results = model.predict(image, conf=conf_threshold, iou=iou_threshold, imgsz=image_size)
|
83 |
+
|
84 |
box = results[0].boxes
|
85 |
|
86 |
+
for r in results:
|
87 |
+
im_array = r.plot()
|
88 |
+
im = Image.fromarray(im_array[..., ::-1])
|
89 |
+
|
90 |
|
91 |
+
model2 = pipeline('image-classification','Kaludi/csgo-weapon-classification')
|
92 |
weapon_name, text_detection = response(image)
|
93 |
|
94 |
|
95 |
# xywh = int(results.boxes.xywh)
|
96 |
# x = xywh[0]
|
97 |
# y = xywh[1]
|
98 |
+
|
99 |
+
return im, text_detection, weapon_name
|
|
|
|
|
100 |
|
101 |
|
102 |
inputs = [
|
103 |
+
gr.Image(type="pil", label="Input Image"),
|
104 |
gr.Slider(minimum=320, maximum=1280, value=640,
|
105 |
step=32, label="Image Size"),
|
106 |
gr.Slider(minimum=0.0, maximum=1.0, value=0.3,
|
|
|
109 |
step=0.05, label="IOU Threshold"),
|
110 |
]
|
111 |
|
112 |
+
outputs = [gr.Image( type="pil", label="Output Image"),
|
|
|
113 |
gr.Textbox(label="Result"),
|
114 |
gr.Textbox(label="Weapon Name")
|
115 |
]
|
116 |
|
117 |
+
examples = [[os.path.join(image_directory, "th (5).jpg"),640, 0.3, 0.6],
|
118 |
+
[os.path.join(image_directory, "th (8).jpg"),640, 0.3, 0.6],
|
119 |
+
[os.path.join(image_directory, "th (11).jpg"),640, 0.3, 0.6],
|
120 |
+
[os.path.join(image_directory, "th (3).jpg"),640, 0.3, 0.6]
|
|
|
121 |
]
|
|
|
|
|
122 |
title = 'Weapon Detection Finetuned YOLOv8'
|
|
|
123 |
description = 'Image Size: Defines the image size for inference.\nConfidence Treshold: Sets the minimum confidence threshold for detections.\nIOU Treshold: Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Useful for reducing duplicates.'
|
124 |
|
125 |
+
|
126 |
+
def pil_to_cv2(pil_image):
|
127 |
+
open_cv_image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
128 |
+
return open_cv_image
|
129 |
+
|
130 |
+
|
131 |
+
def process_video(video_path):
|
132 |
+
cap = cv2.VideoCapture(video_path)
|
133 |
+
|
134 |
+
while cap.isOpened():
|
135 |
+
ret, frame = cap.read()
|
136 |
+
if not ret:
|
137 |
+
breakformat
|
138 |
+
|
139 |
+
pil_img = Image.fromarray(frame[..., ::-1])
|
140 |
+
result = model.predict(source=pil_img)
|
141 |
+
for r in result:
|
142 |
+
im_array = r.plot()
|
143 |
+
processed_frame = Image.fromarray(im_array[..., ::-1])
|
144 |
+
yield processed_frame
|
145 |
+
cap.release()
|
146 |
+
|
147 |
+
|
148 |
+
video_iface = gr.Interface(
|
149 |
+
fn=process_video,
|
150 |
+
inputs=[
|
151 |
+
gr.Video(label="Upload Video", interactive=True)
|
152 |
+
],
|
153 |
+
outputs=gr.Image(type="pil",label="Result"),
|
154 |
+
title=title,
|
155 |
+
description="Upload video for inference."
|
156 |
+
examples=[[os.path.join(video_directory, "ExampleRifle.mp4")],
|
157 |
+
[os.path.join(video_directory, "Knife.mp4")],
|
158 |
+
]
|
159 |
+
)
|
160 |
+
|
161 |
+
|
162 |
+
image_iface = gr.Interface(fn=response2, inputs=inputs, outputs=outputs, examples=examples, title=title, description=description)
|
163 |
+
|
164 |
+
demo = gr.TabbedInterface([image_iface, video_iface], ["Image Inference", "Video Inference"])
|
165 |
+
|
166 |
+
if __name__ == '__main__':
|
167 |
+
demo.launch()
|