Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -30,22 +30,37 @@ def load_datasets():
|
|
30 |
def load_image(image_file):
|
31 |
return Image.open(image_file)
|
32 |
|
|
|
33 |
def classify_image(image):
|
|
|
34 |
img_byte_arr = BytesIO()
|
35 |
image.save(img_byte_arr, format='PNG')
|
36 |
img_byte_arr = img_byte_arr.getvalue()
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
return response.json()
|
47 |
-
|
48 |
-
st.error(f"Image classification failed: {
|
|
|
|
|
49 |
return None
|
50 |
|
51 |
def find_closest_match(df, brand, model):
|
@@ -116,10 +131,12 @@ camera_image = st.camera_input("Take a picture of the car!")
|
|
116 |
|
117 |
if camera_image is not None:
|
118 |
image = load_image(camera_image)
|
119 |
-
st.image(image, caption='Captured Image.', use_container_width=True)
|
120 |
|
121 |
# Classify the car image
|
122 |
-
|
|
|
|
|
123 |
if car_info:
|
124 |
brand = car_info.get('brand', None) # Adjust according to the response structure
|
125 |
model_name = car_info.get('model', None)
|
|
|
30 |
def load_image(image_file):
|
31 |
return Image.open(image_file)
|
32 |
|
33 |
+
|
34 |
def classify_image(image):
|
35 |
+
# Convert PIL Image to bytes
|
36 |
img_byte_arr = BytesIO()
|
37 |
image.save(img_byte_arr, format='PNG')
|
38 |
img_byte_arr = img_byte_arr.getvalue()
|
39 |
|
40 |
+
# Encode image to base64
|
41 |
+
encoded_image = base64.b64encode(img_byte_arr).decode('ascii')
|
42 |
+
|
43 |
+
headers = {
|
44 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_KEY}",
|
45 |
+
"Content-Type": "application/json"
|
46 |
+
}
|
47 |
|
48 |
+
payload = {
|
49 |
+
"inputs": encoded_image
|
50 |
+
}
|
51 |
+
|
52 |
+
try:
|
53 |
+
response = requests.post(
|
54 |
+
'https://api-inference.huggingface.co/models/dima806/car_models_image_detection',
|
55 |
+
headers=headers,
|
56 |
+
json=payload
|
57 |
+
)
|
58 |
+
response.raise_for_status() # Raises an HTTPError for bad responses
|
59 |
return response.json()
|
60 |
+
except requests.exceptions.RequestException as e:
|
61 |
+
st.error(f"Image classification failed: {e}")
|
62 |
+
if response.text:
|
63 |
+
st.error(f"API Response: {response.text}")
|
64 |
return None
|
65 |
|
66 |
def find_closest_match(df, brand, model):
|
|
|
131 |
|
132 |
if camera_image is not None:
|
133 |
image = load_image(camera_image)
|
134 |
+
st.image(image, caption='Captured Image.', use_container_width=True)
|
135 |
|
136 |
# Classify the car image
|
137 |
+
with st.spinner('Classifying image...'):
|
138 |
+
car_info = classify_image(image)
|
139 |
+
|
140 |
if car_info:
|
141 |
brand = car_info.get('brand', None) # Adjust according to the response structure
|
142 |
model_name = car_info.get('model', None)
|