wookimchye commited on
Commit
53d158f
·
verified ·
1 Parent(s): e525f34

Upload 5 files

Browse files
Files changed (2) hide show
  1. app.py +67 -24
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,49 +1,92 @@
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
5
  from huggingface_hub import snapshot_download
6
  import os
 
 
 
7
 
8
  model_path = "best_int8_openvino_model"
9
 
 
 
 
10
 
11
- # Load the model
12
- def load_model():
13
- #download_dir = snapshot_download(repo_id) # download the model from the Hugging Face Hub
14
- #print(download_dir)
15
- #path = os.path.join(download_dir, "best_int8_openvino_model") # path to the model
16
- #print(model_path)
17
- detection_model = YOLO(model_path, task='classify') # load the model
18
  return detection_model
19
 
20
- # Predict the image
21
  def predict(pilimg):
22
  source = pilimg
23
- # x = np.asarray(pilimg)
24
- # print(x.shape)
25
- result = detection_model.predict(source, conf=0.5) # confidence threshold, intersection over union threshold
 
 
 
26
 
27
- #print("Result: ", result)
28
- if not result or len(result[0].boxes) == 0: # if no object detected
29
- gr.Warning("Image not recognized for classfication!")
30
- else:
31
- img_bgr = result[0].plot() # plot the image
32
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
 
 
 
 
33
 
34
- return out_pilimg
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- REPO_ID = "best_int8_openvino_model" # The repo ID of the model
 
 
37
 
38
- detection_model = load_model()
39
 
40
- title = "Classify whether the image is Defective or Good"
41
  interface = gr.Interface(
42
  fn=predict,
43
  inputs=gr.Image(type="pil", label="Input Image"),
44
- outputs=gr.Image(type="pil", label="Classified Imamge"),
45
  title=title,
46
  )
47
 
48
  # Launch the interface
49
  interface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[32]:
5
+
6
+
7
  from ultralytics import YOLO
8
+ from PIL import Image, ImageDraw, ImageFont
9
  import gradio as gr
10
  from huggingface_hub import snapshot_download
11
  import os
12
+ from torchvision import transforms
13
+
14
+ classes = {0: "Defective", 1: "Good"}
15
 
16
  model_path = "best_int8_openvino_model"
17
 
18
+ def load_model_local():
19
+ detection_model = YOLO(model_path, task='classify') # Load the model
20
+ return detection_model
21
 
22
+ def load_model(repo_id):
23
+ download_dir = snapshot_download(repo_id)
24
+ print(download_dir)
25
+ path = os.path.join(download_dir, "best_int8_openvino_model")
26
+ print(path)
27
+ detection_model = YOLO(path, task='classify')
 
28
  return detection_model
29
 
 
30
  def predict(pilimg):
31
  source = pilimg
32
+
33
+ # Call the model to transform image size
34
+ transform = transforms.Compose([
35
+ transforms.Resize((224, 224)),
36
+ transforms.ToTensor(),
37
+ ])
38
 
39
+ source = transform(source) # Update the source image size to 224x224, 1 of 2 sizes accepted by Yolo classification model
40
+
41
+ #result = detection_model.predict(source, conf=0.5, iou=0.6)
42
+ result = detection_model.predict(source) # Make prediction
43
+ # Get the top prediction
44
+ label = result[0].probs.top1
45
+
46
+ class_names = detection_model.names # Retrieves the class names mapping (dict-like)
47
+ classified_type = class_names[label] # Map numeric label to class name
48
+ print (">>> Class : ", classified_type)
49
 
50
+ confidence = result[0].probs.top1conf # Get the top class confidence
51
+ print(">>> Confidence : ", confidence)
52
+
53
+ annotated_image = pilimg.convert("RGB")
54
+ draw = ImageDraw.Draw(annotated_image)
55
+ font = ImageFont.truetype("arialbd.ttf", 30) # Use arial.ttf for bold font
56
+
57
+ if classified_type == classes[0]:
58
+ draw.text((300, 10), classified_type, fill="red", font=font)
59
+ gr.Warning("Defect detected, BAD!.")
60
+ else:
61
+ draw.text((300, 10), classified_type, fill="green", font=font)
62
+ gr.Info("No defect detected,GOOD!")
63
 
64
+ #draw.text((300, 10), classified_type, fill="red", font=font)
65
+
66
+ return annotated_image
67
 
68
+ detection_model = load_model_local()
69
 
70
+ title = "Detect the status of the cap, DEFECTIVE or GOOD"
71
  interface = gr.Interface(
72
  fn=predict,
73
  inputs=gr.Image(type="pil", label="Input Image"),
74
+ outputs=gr.Image(type="pil", label="Classification result"),
75
  title=title,
76
  )
77
 
78
  # Launch the interface
79
  interface.launch(share=True)
80
+
81
+
82
+ # In[ ]:
83
+
84
+
85
+
86
+
87
+
88
+ # In[ ]:
89
+
90
+
91
+
92
+
requirements.txt CHANGED
@@ -2,4 +2,5 @@ ultralytics
2
  gradio
3
  huggingface_hub
4
  pillow
5
- ffmpeg-python
 
 
2
  gradio
3
  huggingface_hub
4
  pillow
5
+ ffmpeg-python
6
+ torchvision