blasterboos commited on
Commit
a38c155
·
1 Parent(s): b9c99ef

Initial Setup

Browse files
Files changed (3) hide show
  1. app.py +51 -23
  2. best.onnx +3 -0
  3. requirements.txt +4 -2
app.py CHANGED
@@ -1,36 +1,64 @@
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
- model_path = "/Users/markk/Downloads/best_int8_openvino_model"
 
8
 
9
- def load_model(repo_id):
10
- download_dir = snapshot_download(repo_id)
11
- print(download_dir)
12
- path = os.path.join(download_dir, "best_int8_openvino_model")
13
- print(path)
14
- detection_model = YOLO(path, task='detect')
15
  return detection_model
16
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- def predict(pilimg):
 
 
 
 
 
 
 
 
 
19
 
20
- source = pilimg
21
- # x = np.asarray(pilimg)
22
- # print(x.shape)
23
- result = detection_model.predict(source, conf=0.5, iou=0.6)
24
- img_bgr = result[0].plot()
25
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
26
-
27
- return out_pilimg
28
 
 
 
 
 
 
 
 
29
 
30
- REPO_ID = "khengkok/balloon_yolov8"
31
- detection_model = load_model(REPO_ID)
 
 
 
32
 
33
- gr.Interface(fn=predict,
34
- inputs=gr.Image(type="pil"),
35
- outputs=gr.Image(type="pil")
36
- ).launch(share=True)
 
1
  from ultralytics import YOLO
2
  from PIL import Image
3
  import gradio as gr
 
4
  import os
5
 
6
+ # Define the model path
7
+ model_path = "C:\Users\Ichiya Rei\235008L\best.onnx"
8
 
9
+
10
+ # Function to load the YOLO model
11
+ def load_model(model_path):
12
+ print(f"Loading model from: {model_path}")
13
+ detection_model = YOLO(model_path, task='detect')
 
14
  return detection_model
15
 
16
+ # Load the YOLO model
17
+ detection_model = load_model(model_path)
18
+
19
+ # Function to perform prediction on an image
20
+ def predict_image(input_img):
21
+ try:
22
+ result = detection_model.predict(input_img, conf=0.5, iou=0.6)
23
+ img_bgr = result[0].plot()
24
+ out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # Convert BGR to RGB for PIL image
25
+ return out_pilimg
26
+ except Exception as e:
27
+ return f"Error processing image: {str(e)}"
28
 
29
+ # Function to perform prediction on a video
30
+ def predict_video(video_path):
31
+ try:
32
+ result_video = detection_model.predict(video_path, conf=0.5, iou=0.6, save=True)
33
+
34
+ # Find the processed video output path
35
+ processed_video_path = str(result_video[0].save_dir / result_video[0].path.name)
36
+ return processed_video_path
37
+ except Exception as e:
38
+ return f"Error processing video: {str(e)}"
39
 
40
+ # Define Gradio interface for image and video separately
41
+ image_interface = gr.Interface(
42
+ fn=predict_image,
43
+ inputs=gr.Image(type="pil"),
44
+ outputs=gr.Image(type="pil"),
45
+ title="Object Detection - Image",
46
+ description="Upload an image and the model will detect objects."
47
+ )
48
 
49
+ video_interface = gr.Interface(
50
+ fn=predict_video,
51
+ inputs=gr.Video(),
52
+ outputs=gr.File(label="Processed Video"),
53
+ title="Object Detection - Video",
54
+ description="Upload a video and the model will detect objects."
55
+ )
56
 
57
+ # Combine both into a tabbed interface
58
+ app = gr.TabbedInterface(
59
+ [image_interface, video_interface],
60
+ ["Image Detection", "Video Detection"]
61
+ )
62
 
63
+ # Launch the app
64
+ app.launch(share=True)
 
 
best.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:389195fb8fd39f37d3f7ec8cebf0cc63a0e6a225dcac0e80350c77efd8bbfea9
3
+ size 103608880
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
- ultralytics
2
- huggingface_hub
 
 
 
1
+ ultralytics==8.3.63
2
+ gradio==5.12.0
3
+ pillow==11.1.0
4
+ onnxruntime==1.20.1