MSanthosh08 commited on
Commit
fc5e239
·
1 Parent(s): 07b2d46

modified app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -39
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import cv2
3
  import requests
4
  import os
5
-
6
  from ultralytics import YOLO
7
 
8
  file_urls = [
@@ -12,36 +11,29 @@ file_urls = [
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('best.pt')
33
- path = [['image_0.jpg'], ['image_1.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
@@ -49,10 +41,10 @@ def show_preds_image(image_path):
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,
@@ -65,29 +57,29 @@ interface_image = gr.Interface(
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,
@@ -101,4 +93,4 @@ interface_video = gr.Interface(
101
  gr.TabbedInterface(
102
  [interface_image, interface_video],
103
  tab_names=['Image inference', 'Video inference']
104
- ).queue().launch()
 
2
  import cv2
3
  import requests
4
  import os
 
5
  from ultralytics import YOLO
6
 
7
  file_urls = [
 
11
  ]
12
 
13
  def download_file(url, save_name):
 
14
  if not os.path.exists(save_name):
15
  file = requests.get(url)
16
+ with open(save_name, 'wb') as f:
17
+ f.write(file.content)
18
 
19
  for i, url in enumerate(file_urls):
20
+ if url.endswith('.mp4') or '.mp4?' in url:
21
+ download_file(url, 'video.mp4')
 
 
 
22
  else:
23
+ download_file(url, f'image_{i}.jpg')
 
 
 
24
 
25
  model = YOLO('best.pt')
26
+ path = [['image_0.jpg'], ['image_1.jpg']]
27
  video_path = [['video.mp4']]
28
 
29
  def show_preds_image(image_path):
30
  image = cv2.imread(image_path)
31
+ results = model(image_path)[0]
32
+ for box in results.boxes.xyxy:
 
33
  cv2.rectangle(
34
  image,
35
+ (int(box[0]), int(box[1])),
36
+ (int(box[2]), int(box[3])),
37
  color=(0, 0, 255),
38
  thickness=2,
39
  lineType=cv2.LINE_AA
 
41
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
42
 
43
  inputs_image = [
44
+ gr.Image(type="filepath", label="Input Image"),
45
  ]
46
  outputs_image = [
47
+ gr.Image(type="numpy", label="Output Image"),
48
  ]
49
  interface_image = gr.Interface(
50
  fn=show_preds_image,
 
57
 
58
  def show_preds_video(video_path):
59
  cap = cv2.VideoCapture(video_path)
60
+ while cap.isOpened():
61
  ret, frame = cap.read()
62
+ if not ret:
63
+ break
64
+ frame_copy = frame.copy()
65
+ results = model(frame_copy)[0]
66
+ for box in results.boxes.xyxy:
67
+ cv2.rectangle(
68
+ frame_copy,
69
+ (int(box[0]), int(box[1])),
70
+ (int(box[2]), int(box[3])),
71
+ color=(0, 0, 255),
72
+ thickness=2,
73
+ lineType=cv2.LINE_AA
74
+ )
75
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
76
+ cap.release()
77
 
78
  inputs_video = [
79
+ gr.Video(label="Input Video"),
 
80
  ]
81
  outputs_video = [
82
+ gr.Image(type="numpy", label="Output Frame"),
83
  ]
84
  interface_video = gr.Interface(
85
  fn=show_preds_video,
 
93
  gr.TabbedInterface(
94
  [interface_image, interface_video],
95
  tab_names=['Image inference', 'Video inference']
96
+ ).queue().launch()