qiqiyuan commited on
Commit
4b7d083
·
verified ·
1 Parent(s): df9c56c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -34
app.py CHANGED
@@ -3,46 +3,45 @@ from PIL import Image
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
5
  import os
6
- import cv2
7
- import tempfile
8
 
9
  #load model
10
- model_path = "best_int8_openvino_model"
11
-
12
  def load_model(repo_id):
 
13
  download_dir = snapshot_download(repo_id)
14
  print(download_dir)
15
- path = os.path.join(download_dir, "best_int8_openvino_model")
16
- print(path)
17
- detection_model = YOLO(path, task='detect')
18
  return detection_model
19
 
20
- REPO_ID = "qiqiyuan/glasses_and_mouth"
 
 
 
 
 
 
 
21
  detection_model = load_model(REPO_ID)
22
 
23
- # Define prediction function for image
24
- def predict(input_data, conf_thresh, iou_thresh):
25
-
26
- if isinstance(input_data, Image.Image): # If input is an image
27
- results = detection_model(input_data, conf=conf_thresh, iou=iou_thresh)
28
- annotated_image = results[0].plot()
29
- return Image.fromarray(annotated_image[..., ::-1]) # Convert BGR to RGB
30
-
31
- else:
32
- return "Invalid input type. Please provide an image."
33
-
34
-
35
- # Create Gradio interface
36
- iface = gr.Interface(
37
- fn=predict,
38
- inputs[
39
- gr.Image(type="pil",label="Image Input"),
40
- gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5, label="Confidence Threshold"),
41
- gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5, label="IoU Threshold")
42
- ],
43
- outputs=gr.Image(type="pil"),
44
- title = "Object detection for glasses and mouth(human) image",
45
- description = "You may adjust confidence and IoU Threshold"
46
- )
47
-
48
- iface.launch(share=True)
 
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
5
  import os
 
 
6
 
7
  #load model
 
 
8
  def load_model(repo_id):
9
+
10
  download_dir = snapshot_download(repo_id)
11
  print(download_dir)
12
+ model_path = os.path.join(download_dir,"best_int8_openvino_model")
13
+ print(model_path)
14
+ detection_model = YOLO(model_path, task='detect')
15
  return detection_model
16
 
17
+ #object detection for image
18
+ def predict(pil_img, conf_thresh, iou_thresh):
19
+ source = pil_img
20
+ results = detection_model.predict(source, conf=conf_thresh, iou=iou_thresh)
21
+ annotated_img = results[0].plot()
22
+ return Image.fromarray(annotated_img[..., ::-1]) # Convert BGR to RGB
23
+
24
+ REPO_ID = "qiqiyuan/glasses_and_mouth"
25
  detection_model = load_model(REPO_ID)
26
 
27
+ #gradio interface
28
+ def iface():
29
+
30
+ interface = gr.Interface(
31
+ fn=predict, # Function to be called
32
+ inputs=[ # List of input components
33
+ gr.Image(type="pil", label="Input Image"),
34
+ gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5, label="Confidence Threshold"),
35
+ gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5, label="IoU Threshold")
36
+ ],
37
+ outputs=gr.Image(type="pil", label="InputImage"), # Output type
38
+ title="Object Detection for Glasses and Mouth (Human)",
39
+ description="Upload an image to detect glasses and mouth (Human) using a pre-trained YOLO model",
40
+ theme="huggingface",
41
+ )
42
+
43
+ return interface
44
+
45
+ #launch the gradio app
46
+ app_iface = iface()
47
+ app_iface.launch(share=True)