mail2kandan commited on
Commit
17f3279
·
1 Parent(s): 28f84c9

Add object detection model for guitars and wristwatches with Gradio interface

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .gradio/certificate.pem
2
+ .venv
3
+
README.md CHANGED
@@ -10,4 +10,33 @@ pinned: false
10
  license: apache-2.0
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  license: apache-2.0
11
  ---
12
 
13
+ # Guitar and Wristwatch Detection Model
14
+
15
+ This Space demonstrates an object detection model trained to identify guitars and wristwatches in images. The model is based on YOLOv8 and has been optimized using OpenVINO.
16
+
17
+ ## Model Details
18
+ - Architecture: YOLOv8
19
+ - Format: OpenVINO
20
+ - Classes: Acoustic Guitar, Wristwatch
21
+ - Input: Images (JPG, PNG)
22
+ - Output: Annotated images with bounding boxes and confidence scores
23
+
24
+ ## Usage
25
+ 1. Upload an image or use one of the example images
26
+ 2. The model will detect and highlight guitars and wristwatches in the image
27
+ 3. Results show bounding boxes with confidence scores
28
+
29
+ ## Examples
30
+ The app includes test images demonstrating detection capabilities:
31
+ - Acoustic guitars (acoustic_guitar_005.jpg, acoustic_guitar_006.jpg)
32
+ - Analog watches (analog_watch_024.jpg, analog_watch_025.jpg)
33
+
34
+ ## Technical Details
35
+ - Built with Gradio 5.6.0
36
+ - Uses Ultralytics YOLO for inference
37
+ - OpenVINO optimization for improved performance
38
+
39
+ ## License
40
+ Apache 2.0
41
+
42
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ from PIL import Image
3
+ import gradio as gr
4
+ from huggingface_hub import hf_hub_download
5
+ import os
6
+
7
+ def load_model(repo_id=None, local_path=None):
8
+ """
9
+ Load YOLO model either from local path or Hugging Face hub.
10
+
11
+ Args:
12
+ repo_id (str, optional): Hugging Face repository ID
13
+ local_path (str, optional): Local path to model directory
14
+
15
+ Returns:
16
+ YOLO: Loaded model
17
+ """
18
+ try:
19
+ if local_path:
20
+ # Load from local path - point to the directory containing the model
21
+ model = YOLO(local_path, task='detect')
22
+ else:
23
+ # Load from Hugging Face hub
24
+ model_path = hf_hub_download(repo_id=repo_id, filename='best_openvino_model')
25
+ model = YOLO(model_path, task='detect')
26
+ return model
27
+ except Exception as e:
28
+ raise Exception(f"Error loading model: {str(e)}")
29
+
30
+ def predict(image, use_local=True):
31
+ """
32
+ Run inference on input image.
33
+
34
+ Args:
35
+ image: Input image (PIL Image)
36
+ use_local (bool): Whether to use local model or Hugging Face model
37
+
38
+ Returns:
39
+ PIL Image: Annotated image with detections
40
+ """
41
+ try:
42
+ # Choose model source based on environment
43
+ if use_local:
44
+ model_path = r'C:\Manikandan\NYP\ITI107\Assignment_draft\output_models\best_openvino_model'
45
+ model = load_model(local_path=model_path)
46
+ else:
47
+ # Use your Hugging Face model repository
48
+ model = load_model(repo_id="mail2kandan/guitar_watch_openvino_model")
49
+
50
+ # Run inference
51
+ results = model.predict(image, conf=0.5, iou=0.6)
52
+
53
+ # Get the plotted image with detections
54
+ img_bgr = results[0].plot()
55
+
56
+ # Convert BGR to RGB for display
57
+ return Image.fromarray(img_bgr[..., ::-1])
58
+
59
+ except Exception as e:
60
+ raise gr.Error(f"Prediction error: {str(e)}")
61
+
62
+ # Create Gradio interface
63
+ def create_interface(use_local=True):
64
+ return gr.Interface(
65
+ fn=lambda img: predict(img, use_local),
66
+ inputs=gr.Image(type="pil"),
67
+ outputs=gr.Image(type="pil"),
68
+ title="Guitar and Wristwatch Detection",
69
+ description="Upload an image to detect guitars and wristwatches.",
70
+ examples=[
71
+ ["testImages/acoustic_guitar_005.jpg"],
72
+ ["testImages/acoustic_guitar_006.jpg"],
73
+ ["testImages/analog_watch_024.jpg"],
74
+ ["testImages/analog_watch_025.jpg"]
75
+ ] if os.path.exists("testImages/acoustic_guitar_005.jpg") else None
76
+ )
77
+
78
+ # Main execution
79
+ if __name__ == "__main__":
80
+ # Determine if we're running on HF Spaces
81
+ is_huggingface = os.environ.get("SPACE_ID") is not None
82
+
83
+ # Create and launch the interface
84
+ interface = create_interface(use_local=not is_huggingface)
85
+ interface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ openvino
3
+ opencv-python
4
+ huggingface_hub
5
+ numpy
6
+ pillow
7
+ ultralytics
testImages/acoustic_guitar_005.jpg ADDED
testImages/acoustic_guitar_006.jpg ADDED
testImages/analog_watch_024.jpg ADDED
testImages/analog_watch_025.jpg ADDED
upload_huggingface.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import HfApi, create_repo
3
+
4
+ model_path = r'C:\Manikandan\NYP\ITI107\Assignment_draft\output_models\best_openvino_model'
5
+ model_name = 'mail2kandan/guitar_watch_openvino_model'
6
+
7
+ # Initialize API
8
+ api = HfApi()
9
+
10
+ # Delete existing repository
11
+ api.delete_repo(repo_id=model_name)
12
+
13
+ # Create repository (replace with your Hugging Face username and model name)
14
+ create_repo(model_name, exist_ok=True)
15
+
16
+ # Upload files
17
+ api.upload_file(
18
+ path_or_fileobj=os.path.join(model_path, 'best.bin'),
19
+ path_in_repo='best.bin',
20
+ repo_id=model_name,
21
+ commit_message="Upload best.bin"
22
+ )
23
+ api.upload_file(
24
+ path_or_fileobj=os.path.join(model_path, 'best.xml'),
25
+ path_in_repo='best.xml',
26
+ repo_id=model_name,
27
+ commit_message="Upload best.xml"
28
+ )
29
+ api.upload_file(
30
+ path_or_fileobj=os.path.join(model_path, 'metadata.yaml'),
31
+ path_in_repo='metadata.yaml',
32
+ repo_id=model_name,
33
+ commit_message="Upload metadata.yaml"
34
+ )