Ihor Bilyk commited on
Commit
3598b74
·
1 Parent(s): 4915cfb
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -1,13 +1,10 @@
1
  import gradio as gr
2
- #import torch
3
  from sahi.prediction import ObjectPrediction
4
  from sahi.utils.cv import visualize_object_predictions, read_image
5
  from ultralyticsplus import YOLO
 
 
6
 
7
- # Images
8
- # torch.hub.download_url_to_file('https://raw.githubusercontent.com/kadirnar/dethub/main/data/images/highway.jpg', 'highway.jpg')
9
- # torch.hub.download_url_to_file('https://user-images.githubusercontent.com/34196005/142742872-1fefcc4d-d7e6-4c43-bbb7-6b5982f7e4ba.jpg', 'highway1.jpg')
10
- # torch.hub.download_url_to_file('https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg', 'small-vehicles1.jpeg')
11
 
12
  def yolov8_inference(
13
  image: gr.inputs.Image = None,
@@ -30,7 +27,14 @@ def yolov8_inference(
30
  model = YOLO(model_path)
31
  model.conf = conf_threshold
32
  model.iou = iou_threshold
33
- results = model.predict(image, imgsz=image_size)
 
 
 
 
 
 
 
34
  object_prediction_list = []
35
  for image_results in results:
36
  if len(image_results)!=0:
@@ -53,7 +57,6 @@ def yolov8_inference(
53
  )
54
  object_prediction_list.append(object_prediction)
55
 
56
- image = read_image(image)
57
  output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
58
  return output_image['image']
59
 
 
1
  import gradio as gr
 
2
  from sahi.prediction import ObjectPrediction
3
  from sahi.utils.cv import visualize_object_predictions, read_image
4
  from ultralyticsplus import YOLO
5
+ import cv2
6
+ from PIL import Image
7
 
 
 
 
 
8
 
9
  def yolov8_inference(
10
  image: gr.inputs.Image = None,
 
27
  model = YOLO(model_path)
28
  model.conf = conf_threshold
29
  model.iou = iou_threshold
30
+
31
+ image = read_image(image)
32
+
33
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
34
+ thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
35
+ thresh = Image.fromarray(thresh)
36
+
37
+ results = model.predict(thresh, imgsz=image_size)
38
  object_prediction_list = []
39
  for image_results in results:
40
  if len(image_results)!=0:
 
57
  )
58
  object_prediction_list.append(object_prediction)
59
 
 
60
  output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
61
  return output_image['image']
62