Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -77,11 +77,30 @@ def _human_label(c):
|
|
| 77 |
return CLASS_LABELS.get(c, str(c))
|
| 78 |
|
| 79 |
# Do the prediction!
|
| 80 |
-
def do_predict(
|
| 81 |
-
#
|
| 82 |
-
if
|
| 83 |
# Return empty results for the outputs if no image is provided
|
| 84 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
try:
|
| 87 |
# IF we have something to work with, save it and prepare the input
|
|
@@ -114,7 +133,8 @@ def do_predict(pil_img: PIL.Image.Image):
|
|
| 114 |
except Exception as e:
|
| 115 |
# Return an error message or empty results if something goes wrong during processing
|
| 116 |
print(f"Error processing image: {e}")
|
| 117 |
-
|
|
|
|
| 118 |
|
| 119 |
|
| 120 |
# Representative example images! These can be local or links.
|
|
|
|
| 77 |
return CLASS_LABELS.get(c, str(c))
|
| 78 |
|
| 79 |
# Do the prediction!
|
| 80 |
+
def do_predict(pil_img_input): # Renamed input parameter for clarity
|
| 81 |
+
# Check the type of the input
|
| 82 |
+
if pil_img_input is None:
|
| 83 |
# Return empty results for the outputs if no image is provided
|
| 84 |
+
return {"no stop sign": 0.0, "stop sign": 0.0}
|
| 85 |
+
|
| 86 |
+
# Handle potential tuple input from webcam
|
| 87 |
+
if isinstance(pil_img_input, tuple):
|
| 88 |
+
print(f"Received tuple input from webcam: {pil_img_input}") # Print for debugging
|
| 89 |
+
# Assuming the tuple contains the image data as the first element
|
| 90 |
+
if pil_img_input and isinstance(pil_img_input[0], PIL.Image.Image):
|
| 91 |
+
pil_img = pil_img_input[0]
|
| 92 |
+
print("Successfully extracted PIL Image from tuple.")
|
| 93 |
+
else:
|
| 94 |
+
# If we can't extract a PIL Image from the tuple
|
| 95 |
+
print("Could not extract PIL Image from tuple input.")
|
| 96 |
+
return {"Error": "Could not process webcam input format."}
|
| 97 |
+
elif isinstance(pil_img_input, PIL.Image.Image):
|
| 98 |
+
pil_img = pil_img_input
|
| 99 |
+
else:
|
| 100 |
+
# Handle unexpected input types
|
| 101 |
+
print(f"Received unexpected input type: {type(pil_img_input)}")
|
| 102 |
+
return {"Error": f"Unexpected input format: {type(pil_img_input)}"}
|
| 103 |
+
|
| 104 |
|
| 105 |
try:
|
| 106 |
# IF we have something to work with, save it and prepare the input
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
# Return an error message or empty results if something goes wrong during processing
|
| 135 |
print(f"Error processing image: {e}")
|
| 136 |
+
# Return a dictionary with error information or default values
|
| 137 |
+
return {"Error": f"Processing failed: {e}"}
|
| 138 |
|
| 139 |
|
| 140 |
# Representative example images! These can be local or links.
|