gouravgujariya commited on
Commit
542dfc0
1 Parent(s): 829db09

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import requests
4
+ import os
5
+
6
+ from ultralytics import YOLO
7
+
8
+ file_urls = ["https://www.dropbox.com/scl/fi/i582tgw95r0f8i8cssmx0/images.jpg?rlkey=fa3d74yaj0bh941jo67n0elns&dl=0"
9
+ # 'https://www.dropbox.com/scl/fi/z4tnnills03s1o4evbqpl/download.jpg?rlkey=gmh63kexnnjcva6ahzo2wfmsd&dl=0'
10
+ ]
11
+
12
+ def download_file(url, save_name):
13
+ url = url
14
+ if not os.path.exists(save_name):
15
+ file = requests.get(url)
16
+ open(save_name, 'wb').write(file.content)
17
+
18
+ for i, url in enumerate(file_urls):
19
+ if 'mp4' in file_urls[i]:
20
+ download_file(
21
+ file_urls[i],
22
+ f"video.mp4"
23
+ )
24
+ else:
25
+ download_file(
26
+ file_urls[i],
27
+ f"image_{i}.jpg"
28
+ )
29
+
30
+ model = YOLO('best.pt')
31
+ path = [['images.jpg'],['images_1.png'],['image/i1.png'],['image/i2.png'],['image/i3.png']]
32
+ # ,['image/i4.png'],['image/i5.png'],['image/i6.png'],['image/i7.png'],['image/i8.png'],['image/i9.png'],['image/i10.png'],['image/i11.png'],
33
+ # ['image/i12.png'],['image/i13.png'],['image/i14.png'],['image/i15.png'],['image/i16.png'],['image/i17.png'],['image/i18.png'],['image/i19.png'],
34
+ # ['image/i20.png'],['image/i21.png'],['image/i22.png'],['image/i23.png'],['image/i24.png'],['image/i25.png'],['image/i26.png'],['image/i27.png'],['image/i28.png']]
35
+ # video_path = [['video.mp4']]
36
+
37
+ def show_preds_image(image_path):
38
+ image = cv2.imread(image_path)
39
+ outputs = model.predict(source=image_path)
40
+ results = outputs[0].cpu().numpy()
41
+ for i, det in enumerate(results.boxes.xyxy):
42
+ cv2.rectangle(
43
+ image,
44
+ (int(det[0]), int(det[1])),
45
+ (int(det[2]), int(det[3])),
46
+ color=(0, 0, 255),
47
+ thickness=2,
48
+ lineType=cv2.LINE_AA
49
+ )
50
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
51
+
52
+ inputs_image = [
53
+ gr.components.Image(type="filepath", label="Input Image"),
54
+ ]
55
+ outputs_image = [
56
+ gr.components.Image(type="numpy", label="Output Image"),
57
+ ]
58
+ interface_image = gr.Interface(
59
+ fn=show_preds_image,
60
+ inputs=inputs_image,
61
+ outputs=outputs_image,
62
+ title="Airport Luggage Weapon Detector app",
63
+ examples=path,
64
+ cache_examples=False,
65
+ )
66
+
67
+ def show_preds_video(video_path):
68
+ cap = cv2.VideoCapture(video_path)
69
+ while(cap.isOpened()):
70
+ ret, frame = cap.read()
71
+ if ret:
72
+ frame_copy = frame.copy()
73
+ outputs = model.predict(source=frame)
74
+ results = outputs[0].cpu().numpy()
75
+ for i, det in enumerate(results.boxes.xyxy):
76
+ cv2.rectangle(
77
+ frame_copy,
78
+ (int(det[0]), int(det[1])),
79
+ (int(det[2]), int(det[3])),
80
+ color=(0, 0, 255),
81
+ thickness=2,
82
+ lineType=cv2.LINE_AA
83
+ )
84
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
85
+
86
+ inputs_video = [
87
+ gr.components.Video(),
88
+
89
+ ]
90
+ outputs_video = [
91
+ gr.components.Image(),
92
+ ]
93
+ interface_video = gr.Interface(
94
+ fn=show_preds_video,
95
+ inputs=inputs_video,
96
+ outputs=outputs_video,
97
+ title="Airport Luggage Weapon Detector",
98
+ cache_examples=False,
99
+ )
100
+
101
+ MORE = """ ## TRY Other Models
102
+ ![imagea](image_path 'image/i5.png')
103
+ ### Instant Image: 4k images in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Image
104
+ """
105
+
106
+ gr.Markdown(MORE)
107
+
108
+ gr.TabbedInterface(
109
+ [interface_image, interface_video],
110
+ tab_names=['Image inference', 'Video inference']
111
+ ).queue().launch()