Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
print("import io")
|
2 |
import io
|
3 |
|
@@ -13,8 +16,8 @@ import base64
|
|
13 |
print("import opencv")
|
14 |
import cv2
|
15 |
|
16 |
-
print("import
|
17 |
-
import
|
18 |
|
19 |
print("import gradio")
|
20 |
import gradio as gr
|
@@ -35,10 +38,71 @@ print("import scipy")
|
|
35 |
from scipy.ndimage import gaussian_filter
|
36 |
|
37 |
print("Initialize Roboflow")
|
|
|
|
|
38 |
rf = Roboflow(api_key="MjzjT2w8u8tlxjmUYDAd")
|
39 |
project = rf.workspace().project("sku-110k")
|
40 |
model = project.version(2).model
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
def resize_image(image, max_size=1500):
|
43 |
if max(image.size) > max_size:
|
44 |
ratio = max_size / float(max(image.size))
|
@@ -49,17 +113,7 @@ def resize_image(image, max_size=1500):
|
|
49 |
buffered = buffer.getvalue()
|
50 |
return base64.b64encode(buffered).decode("utf-8")
|
51 |
|
52 |
-
|
53 |
-
base_url = f"https://detect.roboflow.com/{model}/{version}?api_key={api_key}&confidence={confidence}&overlap={overlap}&format={format}"
|
54 |
-
if format == "image" and labels:
|
55 |
-
base_url += "&labels=on"
|
56 |
-
base_url += f"&stroke={stroke}"
|
57 |
-
image_data = resize_image(image)
|
58 |
-
response = requests.post(base_url, data=image_data, headers={"Content-Type": "application/x-www-form-urlencoded"})
|
59 |
-
if format == "json":
|
60 |
-
return response.json()
|
61 |
-
elif format == "image":
|
62 |
-
return Image.open(io.BytesIO(response.content))
|
63 |
|
64 |
title = "<center>Cigarette Pack Counter</center>"
|
65 |
description = "<center><a href='http://counttek.online'><img width='25%' height='25%' src='https://mvp-83056e96f7ab.herokuapp.com/static/countteklogo2.png'></a><br><a href='https://nolenfelten.github.io'>Project by Nolen Felten</a></center>"
|
|
|
1 |
+
print("import os")
|
2 |
+
import os
|
3 |
+
|
4 |
print("import io")
|
5 |
import io
|
6 |
|
|
|
16 |
print("import opencv")
|
17 |
import cv2
|
18 |
|
19 |
+
print("import tempfile")
|
20 |
+
import tempfile
|
21 |
|
22 |
print("import gradio")
|
23 |
import gradio as gr
|
|
|
38 |
from scipy.ndimage import gaussian_filter
|
39 |
|
40 |
print("Initialize Roboflow")
|
41 |
+
|
42 |
+
|
43 |
rf = Roboflow(api_key="MjzjT2w8u8tlxjmUYDAd")
|
44 |
project = rf.workspace().project("sku-110k")
|
45 |
model = project.version(2).model
|
46 |
|
47 |
+
def encode_image(image_path):
|
48 |
+
"""
|
49 |
+
Encode an image located at image_path to base64 string.
|
50 |
+
|
51 |
+
Parameters:
|
52 |
+
- image_path: Path to the image file.
|
53 |
+
|
54 |
+
Returns:
|
55 |
+
A base64 encoded string representing the image.
|
56 |
+
"""
|
57 |
+
with open(image_path, "rb") as image_file:
|
58 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
59 |
+
|
60 |
+
|
61 |
+
def gradio_infer(image, model_name, model_version, confidence, overlap, api_key, output_format, include_labels, stroke_width):
|
62 |
+
'''
|
63 |
+
Send the image to Roboflow API for inference.
|
64 |
+
Returns JSON and image with bounding boxes drawn on to it.
|
65 |
+
'''
|
66 |
+
# Save the incoming image to a temporary file
|
67 |
+
temp_dir = tempfile.mkdtemp()
|
68 |
+
temp_file_path = os.path.join(temp_dir, "temp_image.jpg")
|
69 |
+
image.save(temp_file_path)
|
70 |
+
|
71 |
+
json_url = f"https://detect.roboflow.com/{model_name}/{model_version}?api_key=MjzjT2w8u8tlxjmUYDAd&confidence={confidence}&overlap={overlap}&format=json"
|
72 |
+
image_url = f"https://detect.roboflow.com/{model_name}/{model_version}?api_key=MjzjT2w8u8tlxjmUYDAd&confidence={confidence}&overlap={overlap}&format=image&labels={str(include_labels).lower()}&stroke={stroke_width}"
|
73 |
+
|
74 |
+
encoded_image = encode_image(temp_file_path)
|
75 |
+
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
76 |
+
|
77 |
+
json_request = requests.post(json_url, data=encoded_image, headers=headers)
|
78 |
+
image_request = requests.post(image_url, data=encoded_image, headers=headers)
|
79 |
+
|
80 |
+
print("JSON Response Headers:", json_request.headers)
|
81 |
+
print("Image Response Headers:", image_request.headers)
|
82 |
+
|
83 |
+
json_response = {}
|
84 |
+
image_response = None
|
85 |
+
|
86 |
+
if json_request.status_code == 200:
|
87 |
+
try:
|
88 |
+
json_response = json_request.json()
|
89 |
+
except json.JSONDecodeError:
|
90 |
+
json_response = {"error": "Invalid JSON response"}
|
91 |
+
else:
|
92 |
+
json_response = {"error": f"Failed to get JSON response, status code: {json_request.status_code}"}
|
93 |
+
|
94 |
+
if image_request.status_code == 200 and 'image' in image_request.headers.get('Content-Type', ''):
|
95 |
+
try:
|
96 |
+
image_response = Image.open(io.BytesIO(image_request.content))
|
97 |
+
except Exception as e:
|
98 |
+
image_response = None
|
99 |
+
print(f"Failed to open image: {e}")
|
100 |
+
else:
|
101 |
+
print(f"Failed to retrieve image, status code: {image_request.status_code}")
|
102 |
+
print("Image Response Content:", image_request.content)
|
103 |
+
|
104 |
+
return image_response, json_response
|
105 |
+
|
106 |
def resize_image(image, max_size=1500):
|
107 |
if max(image.size) > max_size:
|
108 |
ratio = max_size / float(max(image.size))
|
|
|
113 |
buffered = buffer.getvalue()
|
114 |
return base64.b64encode(buffered).decode("utf-8")
|
115 |
|
116 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
title = "<center>Cigarette Pack Counter</center>"
|
119 |
description = "<center><a href='http://counttek.online'><img width='25%' height='25%' src='https://mvp-83056e96f7ab.herokuapp.com/static/countteklogo2.png'></a><br><a href='https://nolenfelten.github.io'>Project by Nolen Felten</a></center>"
|