dexpyw commited on
Commit
549bd9c
·
verified ·
1 Parent(s): e6f6d5e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +25 -15
  2. requirements.txt +2 -1
app.py CHANGED
@@ -3,7 +3,7 @@ from PIL import Image
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
5
  import os
6
-
7
 
8
  def load_model(repo_id):
9
  download_dir = snapshot_download(repo_id)
@@ -14,22 +14,32 @@ def load_model(repo_id):
14
  return detection_model
15
 
16
 
17
- def predict(pilimg):
18
-
19
- source = pilimg
20
- # x = np.asarray(pilimg)
21
- # print(x.shape)
22
- result = detection_model.predict(source, conf=0.5, iou=0.6)
23
- img_bgr = result[0].plot()
24
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
25
-
26
- return out_pilimg
 
 
 
 
 
 
 
 
27
 
28
 
29
  REPO_ID = "dexpyw/model"
30
  detection_model = load_model(REPO_ID)
31
 
32
- gr.Interface(fn=predict,
33
- inputs=gr.Image(type="pil"),
34
- outputs=gr.Image(type="pil")
35
- ).launch(share=True)
 
 
 
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
5
  import os
6
+ import cv2
7
 
8
  def load_model(repo_id):
9
  download_dir = snapshot_download(repo_id)
 
14
  return detection_model
15
 
16
 
17
+ def predict(input_data):
18
+ if isinstance(input_data, Image.Image):
19
+ source = input_data
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])
23
+ return out_pilimg
24
+ elif isinstance(input_data, str) and input_data.endswith('.mp4'):
25
+ cap = cv2.VideoCapture(input_data)
26
+ ret, frame = cap.read()
27
+ if ret:
28
+ result = detection_model.predict(frame, conf=0.5, iou=0.6)
29
+ img_bgr = result[0].plot()
30
+ out_pilimg = Image.fromarray(img_bgr[..., ::-1])
31
+ cap.release()
32
+ return out_pilimg
33
+ cap.release()
34
+ return None
35
 
36
 
37
  REPO_ID = "dexpyw/model"
38
  detection_model = load_model(REPO_ID)
39
 
40
+ gr.Interface(
41
+ fn=predict,
42
+ inputs=[gr.Image(type="pil", label="Upload Image"), gr.Video(type="file", label="Upload Video")],
43
+ outputs=gr.Image(type="pil", label="Predicted Output"),
44
+ live=True
45
+ ).launch(share=True)
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  ultralytics
2
- huggingface_hub
 
 
1
  ultralytics
2
+ huggingface_hub
3
+ opencv-python