manvi23 commited on
Commit
27b0fa8
·
verified ·
1 Parent(s): d088e8d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +56 -58
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,58 +1,56 @@
1
- from clearml import Model
2
- from ultralytics import YOLO
3
- from PIL import Image, ImageDraw
4
- import gradio as gr
5
-
6
- # fetching the model from the ClearML
7
- def fetch_model_from_clearml(model_id):
8
- try:
9
- # Load the model from ClearML
10
- model = Model(model_id=model_id)
11
- model_path = model.get_local_copy()
12
- print(f"Model successfully fetched: {model_path}")
13
- return model_path
14
- except Exception as e:
15
- print(f"Failed to fetch model from ClearML: {e}")
16
- raise
17
-
18
- model_id = "65ca2d5aa3384a68b13333f792b147a3"
19
- model_path = fetch_model_from_clearml(model_id)
20
-
21
- model = YOLO(model_path)
22
-
23
-
24
- def predict_and_visualize(image, confidence_threshold=0.3):
25
- results = model.predict(image, conf=confidence_threshold)
26
-
27
- boxes = results[0].boxes.xyxy.cpu().numpy() # Bounding box coordinates
28
- scores = results[0].boxes.conf.cpu().numpy() # Confidence scores
29
-
30
- # Draw bounding boxes on the image
31
- draw = ImageDraw.Draw(image)
32
- for box, score in zip(boxes, scores):
33
- x_min, y_min, x_max, y_max = box
34
- draw.rectangle([x_min, y_min, x_max, y_max], outline="red", width=3)
35
- draw.text((x_min, y_min), f"{score:.2f}", fill="red")
36
-
37
- return image
38
-
39
-
40
- # Gradio interface function
41
- def gradio_app(image):
42
- result_image = predict_and_visualize(image)
43
- return result_image
44
-
45
-
46
- # Create Gradio app
47
- interface = gr.Interface(
48
- fn=gradio_app,
49
- inputs=[
50
- gr.Image(type="pil"), # Input image
51
- ],
52
- outputs=gr.Image(type="pil"), # Output image
53
- title="Object Detection with model of Group G",
54
- description="Upload an image to detect the objects.",
55
- )
56
-
57
- # Launch the Gradio app
58
- interface.launch(share=True)
 
1
+ from clearml import Model
2
+ from ultralytics import YOLO
3
+ from PIL import Image, ImageDraw
4
+ from huggingface_hub import hf_hub_download
5
+ import gradio as gr
6
+
7
+
8
+ # fetching the model from the ClearML
9
+ def fetch_model_from_HG():
10
+ try:
11
+ hg_model = hf_hub_download(repo_id="manvi23/GroupG", filename="best.pt")
12
+ return hg_model
13
+ except Exception as e:
14
+ print(f"Failed to fetch model from HuggingFace: {e}")
15
+ raise
16
+
17
+ model_path = fetch_model_from_HG()
18
+ model = YOLO(model_path)
19
+
20
+
21
+ def predict_and_visualize(image, confidence_threshold=0.3):
22
+ results = model.predict(image, conf=confidence_threshold)
23
+
24
+ boxes = results[0].boxes.xyxy.cpu().numpy() # Bounding box coordinates
25
+ scores = results[0].boxes.conf.cpu().numpy() # Confidence scores
26
+
27
+ # Draw bounding boxes on the image
28
+ draw = ImageDraw.Draw(image)
29
+ for box, score in zip(boxes, scores):
30
+ x_min, y_min, x_max, y_max = box
31
+ draw.rectangle([x_min, y_min, x_max, y_max], outline="red", width=3)
32
+ draw.text((x_min, y_min), f"{score:.2f}", fill="red")
33
+
34
+ return image
35
+
36
+
37
+ # Gradio interface function
38
+ def gradio_app(image):
39
+ result_image = predict_and_visualize(image)
40
+ return result_image
41
+
42
+
43
+ # Create Gradio app
44
+ interface = gr.Interface(
45
+ fn=gradio_app,
46
+ inputs=[
47
+ gr.Image(type="pil"), # Input image
48
+ ],
49
+ outputs=gr.Image(type="pil"), # Output image
50
+ title="Object Detection with model of Group G",
51
+ description="Upload an image to detect the objects.",
52
+ )
53
+
54
+ # Launch the Gradio app
55
+ interface.launch(share=True)
56
+
 
 
requirements.txt CHANGED
@@ -4,4 +4,4 @@ clearml==1.16.4
4
  ultralytics==8.3.49
5
  numpy==1.26.4
6
  pillow==11.0.0
7
-
 
4
  ultralytics==8.3.49
5
  numpy==1.26.4
6
  pillow==11.0.0
7
+ huggingface_hub