D3V1L1810 commited on
Commit
1ddbf19
·
verified ·
1 Parent(s): 668ccfa

Upload 3 files

Browse files
Files changed (3) hide show
  1. Bottles_Cans_Classify_v1.pt +3 -0
  2. app.py +72 -0
  3. requirements.txt +1 -0
Bottles_Cans_Classify_v1.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:39cc9a16cba13972131ce143fcc4894ce7ea631348cb556199de1a3f5984cd0d
3
+ size 31700953
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from ultralytics import YOLO
4
+ import requests
5
+ import json
6
+ import logging
7
+
8
+ logging.basicConfig(level=logging.INFO)
9
+
10
+ model = YOLO("Bottles_Cans_Classify_v1.pt")
11
+
12
+ def detect_objects(images):
13
+ results = model(images)
14
+ classes={ 0:"bottle_25cl", 1:"bottle_33cl", 2:"bottle_50cl", 3:"bottle_100cl", 4:"bottle_150cl", 5:"bottle_200cl", 6:"can", 7:"reject" }
15
+ names=[]
16
+ for result in results:
17
+ probs = result.probs.top1
18
+ names.append(classes[probs])
19
+ return names
20
+
21
+ def create_solutions(image_urls, names):
22
+ solutions = []
23
+ for image_url, prediction in zip(image_urls, names):
24
+ prediction_list=[]
25
+ prediction_list.append(prediction)
26
+ obj = {"url": image_url, "answer": prediction_list}
27
+ solutions.append(obj)
28
+ return solutions
29
+
30
+ # def send_results_to_api(data, result_url):
31
+ # # Example function to send results to an API
32
+ # headers = {"Content-Type": "application/json"}
33
+ # response = requests.post(result_url, json=data, headers=headers)
34
+ # if response.status_code == 200:
35
+ # return response.json() # Return any response from the API if needed
36
+ # else:
37
+ # return {"error": f"Failed to send results to API: {response.status_code}"}
38
+
39
+ def process_images(params):
40
+ try:
41
+ params = json.loads(params)
42
+ except json.JSONDecodeError as e:
43
+ logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
44
+ return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
45
+
46
+ image_urls = params.get("urls", [])
47
+ # api = params.get("api", "")
48
+ # job_id = params.get("job_id", "")
49
+
50
+ if not image_urls:
51
+ logging.error("Missing required parameters: 'urls'")
52
+ return {"error": "Missing required parameters: 'urls'"}
53
+
54
+ try:
55
+ images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
56
+ except Exception as e:
57
+ logging.error(f"Error loading images: {e}")
58
+ return {"error": f"Error loading images: {str(e)}"}
59
+
60
+ names = detect_objects(images) # Perform object detection
61
+ solutions = create_solutions(image_urls, names) # Create solutions with image URLs and bounding boxes
62
+
63
+ # result_url = f"{api}/{job_id}"
64
+ # send_results_to_api(solutions, result_url)
65
+
66
+ return json.dumps({"solutions": solutions})
67
+
68
+ inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'img_url':['a.jpg','b.jpg']}")
69
+ outputs = gr.JSON()
70
+
71
+ application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Bottles Cans Classification with API Integration")
72
+ application.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ultralytics