CharmainChua commited on
Commit
a97d6e0
·
1 Parent(s): 1e74261

changes to requirements and app.py for video uplaod

Browse files
Files changed (2) hide show
  1. app.py +49 -15
  2. requirements.txt +3 -0
app.py CHANGED
@@ -3,35 +3,69 @@ from PIL import Image
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
5
  import os
6
-
7
- model_path = "/Users/charmchua/Desktop/Y3S2/ATA/ATA_ASSN2/224684P/best_int8_openvino_model"
8
 
9
  def load_model(repo_id):
10
  download_dir = snapshot_download(repo_id)
11
  print(download_dir)
12
- path = os.path.join(download_dir, "best_int8_openvino_model")
13
  print(path)
14
  detection_model = YOLO(path, task='detect')
15
  return detection_model
16
 
17
-
18
- def predict(pilimg):
19
-
20
  source = pilimg
21
- # x = np.asarray(pilimg)
22
- # print(x.shape)
23
  result = detection_model.predict(source, conf=0.5, iou=0.6)
24
  img_bgr = result[0].plot()
25
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
26
-
27
  return out_pilimg
28
 
 
 
 
 
 
 
29
 
30
- REPO_ID = "CharmainChua/windowsandcurtains"
 
 
 
 
 
 
 
 
 
 
 
31
 
 
 
 
 
 
 
 
 
 
32
  detection_model = load_model(REPO_ID)
33
 
34
- gr.Interface(fn=predict,
35
- inputs=gr.Image(type="pil"),
36
- outputs=gr.Image(type="pil")
37
- ).launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
5
  import os
6
+ import cv2
7
+ import tempfile
8
 
9
  def load_model(repo_id):
10
  download_dir = snapshot_download(repo_id)
11
  print(download_dir)
12
+ path = os.path.join(download_dir, "best_int8_openvino_model")
13
  print(path)
14
  detection_model = YOLO(path, task='detect')
15
  return detection_model
16
 
17
+ def predict_image(pilimg):
18
+ # Process image
 
19
  source = pilimg
 
 
20
  result = detection_model.predict(source, conf=0.5, iou=0.6)
21
  img_bgr = result[0].plot()
22
+ out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # Convert BGR to RGB
 
23
  return out_pilimg
24
 
25
+ def predict_video(video_path):
26
+ # Read video file
27
+ cap = cv2.VideoCapture(video_path)
28
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
29
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
30
+ fps = cap.get(cv2.CAP_PROP_FPS)
31
 
32
+ # Create temporary output file
33
+ temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
34
+ out = cv2.VideoWriter(temp_video.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
35
+
36
+ while cap.isOpened():
37
+ ret, frame = cap.read()
38
+ if not ret:
39
+ break
40
+
41
+ # Run YOLO prediction on each frame
42
+ result = detection_model.predict(frame, conf=0.5, iou=0.6)
43
+ frame_with_boxes = result[0].plot()
44
 
45
+ # Write processed frame to output video
46
+ out.write(frame_with_boxes)
47
+
48
+ cap.release()
49
+ out.release()
50
+
51
+ return temp_video.name
52
+
53
+ REPO_ID = "CharmainChua/windowsandcurtains"
54
  detection_model = load_model(REPO_ID)
55
 
56
+ # Gradio Interface
57
+ image_input = gr.Interface(
58
+ fn=predict_image,
59
+ inputs=gr.Image(type="pil"),
60
+ outputs=gr.Image(type="pil"),
61
+ label="Object Detection on Image"
62
+ )
63
+
64
+ video_input = gr.Interface(
65
+ fn=predict_video,
66
+ inputs=gr.Video(type="file"),
67
+ outputs=gr.Video(),
68
+ label="Object Detection on Video"
69
+ )
70
+
71
+ gr.TabbedInterface([image_input, video_input], ["Image Detection", "Video Detection"]).launch(share=True)
requirements.txt CHANGED
@@ -1,2 +1,5 @@
1
  ultralytics
2
  huggingface_hub
 
 
 
 
1
  ultralytics
2
  huggingface_hub
3
+ gradio
4
+ opencv-python
5
+ Pillow