KendrickTan commited on
Commit
f8903f8
·
verified ·
1 Parent(s): 7b2d00b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -26
app.py CHANGED
@@ -1,34 +1,21 @@
1
  from ultralytics import YOLO
2
  from PIL import Image
3
  import gradio as gr
4
- from huggingface_hub import snapshot_download
5
- import os
6
-
7
- def load_model(repo_id):
8
- download_dir = snapshot_download(repo_id)
9
- print(download_dir)
10
- path = os.path.join(download_dir, "best_int8_openvino_model")
11
- print(path)
12
- detection_model = YOLO(path, task='detect')
13
- return detection_model
14
 
 
 
15
 
 
16
  def predict(pilimg):
17
-
18
- source = pilimg
19
- # x = np.asarray(pilimg)
20
- # print(x.shape)
21
- result = detection_model.predict(source, conf=0.5, iou=0.6)
22
- img_bgr = result[0].plot()
23
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
24
-
25
  return out_pilimg
26
 
27
-
28
- REPO_ID = "KendrickTan/231941k"
29
- detection_model = load_model(REPO_ID)
30
-
31
- gr.Interface(fn=predict,
32
- inputs=gr.Image(type="pil"),
33
- outputs=gr.Image(type="pil")
34
- ).launch(share=True)
 
1
  from ultralytics import YOLO
2
  from PIL import Image
3
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Load model directly from file in the Space
6
+ detection_model = YOLO("best.pt")
7
 
8
+ # Prediction function
9
  def predict(pilimg):
10
+ results = detection_model.predict(pilimg, conf=0.5, iou=0.6)
11
+ img_bgr = results[0].plot()
12
+ out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # Convert BGR to RGB
 
 
 
 
 
13
  return out_pilimg
14
 
15
+ # Gradio interface
16
+ gr.Interface(
17
+ fn=predict,
18
+ inputs=gr.Image(type="pil"),
19
+ outputs=gr.Image(type="pil"),
20
+ title="Mask Detection Demo"
21
+ ).launch()