kaitongg commited on
Commit
5eac2fb
·
verified ·
1 Parent(s): fe35c19

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +25 -5
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(pil_img: PIL.Image.Image):
81
- # Make sure there's actually an image to work with
82
- if pil_img is None:
83
  # Return empty results for the outputs if no image is provided
84
- return {}, pandas.DataFrame(columns=["Predicted label", "Confidence (%)"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- return {}, pandas.DataFrame(columns=["Predicted label", "Confidence (%)"])
 
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.