wookimchye commited on
Commit
66f13bf
·
verified ·
1 Parent(s): 1f9184a

Upload 3 files

Browse files
Files changed (3) hide show
  1. DurianMangosteen1.jpg +0 -0
  2. DurianMangosteen2.jpg +0 -0
  3. app.py +20 -19
DurianMangosteen1.jpg ADDED
DurianMangosteen2.jpg ADDED
app.py CHANGED
@@ -1,9 +1,4 @@
1
- #!/usr/bin/env python
2
- # coding: utf-8
3
-
4
- # In[4]:
5
-
6
-
7
  from ultralytics import YOLO
8
  from PIL import Image
9
  import gradio as gr
@@ -12,28 +7,35 @@ import os
12
 
13
  model_path = "best_int8_openvino_model"
14
 
 
 
 
 
15
  def load_model(repo_id):
16
- download_dir = snapshot_download(repo_id)
17
  print(download_dir)
18
- path = os.path.join(download_dir, "best_int8_openvino_model")
19
  print(path)
20
- detection_model = YOLO(path, task='detect')
21
  return detection_model
22
 
23
-
24
  def predict(pilimg):
25
-
26
  source = pilimg
27
  # x = np.asarray(pilimg)
28
  # print(x.shape)
29
- result = detection_model.predict(source, conf=0.4, iou=0.6)
30
- img_bgr = result[0].plot()
31
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
32
 
33
- return out_pilimg
34
-
 
 
 
 
 
 
35
 
36
- REPO_ID = "ITI107-2024S2/8035531F"
37
  detection_model = load_model(REPO_ID)
38
 
39
  title = "Detect Durian and Mangosteen (King and Queen of Fruits) In The Image"
@@ -42,14 +44,13 @@ interface = gr.Interface(
42
  inputs=gr.Image(type="pil", label="Input Image"),
43
  outputs=gr.Image(type="pil", label="Object Detected Image"),
44
  title=title,
 
45
  )
46
 
47
  # Launch the interface
48
  interface.launch(share=True)
49
 
50
 
51
- # In[ ]:
52
-
53
 
54
 
55
 
 
1
+ # Description: This is the main file to run the Gradio interface for the object detection model.
 
 
 
 
 
2
  from ultralytics import YOLO
3
  from PIL import Image
4
  import gradio as gr
 
7
 
8
  model_path = "best_int8_openvino_model"
9
 
10
+ # Example paths for Gradio
11
+ image_examples = [["DurianMangosteen1.jpg"], ["DurianMangosteen2.jpg"]]
12
+
13
+ # Load the model
14
  def load_model(repo_id):
15
+ download_dir = snapshot_download(repo_id) # download the model from the Hugging Face Hub
16
  print(download_dir)
17
+ path = os.path.join(download_dir, "best_int8_openvino_model") # path to the model
18
  print(path)
19
+ detection_model = YOLO(path, task='detect') # load the model
20
  return detection_model
21
 
22
+ # Predict the image
23
  def predict(pilimg):
 
24
  source = pilimg
25
  # x = np.asarray(pilimg)
26
  # print(x.shape)
27
+ result = detection_model.predict(source, conf=0.4, iou=0.6) # confidence threshold, intersection over union threshold
 
 
28
 
29
+ #print("Result: ", result)
30
+ if not result or len(result[0].boxes) == 0: # if no object detected
31
+ gr.Warning("No object detected in the image!")
32
+ else:
33
+ img_bgr = result[0].plot() # plot the image
34
+ out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
35
+
36
+ return out_pilimg
37
 
38
+ REPO_ID = "ITI107-2024S2/8035531F" # The repo ID of the model
39
  detection_model = load_model(REPO_ID)
40
 
41
  title = "Detect Durian and Mangosteen (King and Queen of Fruits) In The Image"
 
44
  inputs=gr.Image(type="pil", label="Input Image"),
45
  outputs=gr.Image(type="pil", label="Object Detected Image"),
46
  title=title,
47
+ examples=image_examples,
48
  )
49
 
50
  # Launch the interface
51
  interface.launch(share=True)
52
 
53
 
 
 
54
 
55
 
56