ranggaaldosas commited on
Commit
9ec6d86
1 Parent(s): 38a3d50

feat: fix app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -43
app.py CHANGED
@@ -5,8 +5,14 @@ import os
5
 
6
  from ultralytics import YOLO
7
 
8
- model = YOLO('best_model.pt')
9
- example_list = [["examples/" + example] for example in os.listdir("examples")]
 
 
 
 
 
 
10
 
11
  def show_preds_image(image_path):
12
  image = cv2.imread(image_path)
@@ -19,61 +25,58 @@ def show_preds_image(image_path):
19
  (int(det[2]), int(det[3])),
20
  color=(0, 0, 255),
21
  thickness=2,
22
- lineType=cv2.LINE_AA
23
  )
24
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
25
 
26
- inputs_image = [
27
- gr.components.Image(type="filepath", label="Input Image"),
28
- ]
29
- outputs_image = [
30
- gr.components.Image(type="numpy", label="Output Image"),
31
- ]
 
 
 
 
 
 
 
 
 
 
 
 
32
  interface_image = gr.Interface(
33
  fn=show_preds_image,
34
- inputs=inputs_image,
35
- outputs=outputs_image,
36
- title="Pothole detector",
37
- examples=path,
38
  cache_examples=False,
39
  )
40
 
 
 
 
41
  def show_preds_video(video_path):
42
- cap = cv2.VideoCapture(video_path)
43
- while(cap.isOpened()):
44
- ret, frame = cap.read()
45
- if ret:
46
- frame_copy = frame.copy()
47
- outputs = model.predict(source=frame)
48
- results = outputs[0].cpu().numpy()
49
- for i, det in enumerate(results.boxes.xyxy):
50
- cv2.rectangle(
51
- frame_copy,
52
- (int(det[0]), int(det[1])),
53
- (int(det[2]), int(det[3])),
54
- color=(0, 0, 255),
55
- thickness=2,
56
- lineType=cv2.LINE_AA
57
- )
58
- yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
59
 
60
- inputs_video = [
61
- gr.components.Video(type="filepath", label="Input Video"),
62
 
63
- ]
64
- outputs_video = [
65
- gr.components.Image(type="numpy", label="Output Image"),
66
- ]
67
  interface_video = gr.Interface(
68
  fn=show_preds_video,
69
- inputs=inputs_video,
70
- outputs=outputs_video,
71
- title="Pothole detector",
72
- examples=video_path,
73
  cache_examples=False,
74
  )
75
 
 
76
  gr.TabbedInterface(
77
- [interface_image, interface_video],
78
- tab_names=['Image inference', 'Video inference']
79
- ).queue().launch()
 
5
 
6
  from ultralytics import YOLO
7
 
8
+ model = YOLO("best_model.pt")
9
+ example_imgs = [
10
+ os.path.join("example", "img", example) for example in os.listdir("example/img")
11
+ ]
12
+ example_vids = [
13
+ os.path.join("example", "vid", example) for example in os.listdir("example/vid")
14
+ ]
15
+
16
 
17
  def show_preds_image(image_path):
18
  image = cv2.imread(image_path)
 
25
  (int(det[2]), int(det[3])),
26
  color=(0, 0, 255),
27
  thickness=2,
28
+ lineType=cv2.LINE_AA,
29
  )
30
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
31
 
32
+
33
+ def show_preds_image(image_path):
34
+ image = cv2.imread(image_path)
35
+ outputs = model.predict(source=image_path)
36
+ results = outputs[0].cpu().numpy()
37
+ for det in results.boxes.xyxy:
38
+ cv2.rectangle(
39
+ image,
40
+ (int(det[0]), int(det[1])),
41
+ (int(det[2]), int(det[3])),
42
+ color=(0, 0, 255),
43
+ thickness=2,
44
+ lineType=cv2.LINE_AA,
45
+ )
46
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
47
+
48
+
49
+ # Define the Gradio interface for image input
50
  interface_image = gr.Interface(
51
  fn=show_preds_image,
52
+ inputs=gr.components.Image(type="filepath", label="Input Image"),
53
+ outputs=gr.components.Image(type="numpy", label="Output Image"),
54
+ title="Pothole Detector - Image",
55
+ examples=example_imgs,
56
  cache_examples=False,
57
  )
58
 
59
+
60
+ # For video processing, it's best to process and then show the output video.
61
+ # This is a simplified placeholder for video processing, indicating where to include the video processing logic.
62
  def show_preds_video(video_path):
63
+ # Placeholder for video processing function
64
+ # Process the video here and save the output, then return the path to the processed video
65
+ processed_video_path = "processed_video.mp4" # Example output path
66
+ return processed_video_path
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
 
 
68
 
69
+ # Define the Gradio interface for video input
 
 
 
70
  interface_video = gr.Interface(
71
  fn=show_preds_video,
72
+ inputs=gr.components.Video(type="filepath", label="Input Video"),
73
+ outputs=gr.components.Video(label="Processed Video"),
74
+ title="Pothole Detector - Video",
75
+ examples=example_vids,
76
  cache_examples=False,
77
  )
78
 
79
+ # Combine the interfaces into a tabbed interface
80
  gr.TabbedInterface(
81
+ [interface_image, interface_video], tab_names=["Image Inference", "Video Inference"]
82
+ ).launch()