Spaces:
Sleeping
Sleeping
Enhance tools
Browse files- tools.py +7 -1
- vlm_tools.py +22 -14
tools.py
CHANGED
@@ -155,7 +155,13 @@ def load_dataframe_from_excel(file_path: str)->pd.DataFrame:
|
|
155 |
Returns:
|
156 |
The pandas DataFrame
|
157 |
"""
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
@tool
|
161 |
def to_dataframe(data: List[dict], columns: List[str])->pd.DataFrame:
|
|
|
155 |
Returns:
|
156 |
The pandas DataFrame
|
157 |
"""
|
158 |
+
try:
|
159 |
+
df = pd.read_excel(file_path)
|
160 |
+
except Exception as e:
|
161 |
+
curr_dir = os.path.dirname(os.path.abspath(__file__))
|
162 |
+
file_path = os.path.join(curr_dir, file_path)
|
163 |
+
df = pd.read_excel(file_path)
|
164 |
+
return df
|
165 |
|
166 |
@tool
|
167 |
def to_dataframe(data: List[dict], columns: List[str])->pd.DataFrame:
|
vlm_tools.py
CHANGED
@@ -49,10 +49,17 @@ def pre_processing(image: str, input_size=(416, 416))->tuple:
|
|
49 |
|
50 |
# Convert BGR to RGB and normalize
|
51 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # More reliable than array slicing
|
52 |
-
img = img.transpose(2, 0, 1) # HWC to CHW
|
53 |
-
img = np.expand_dims(img, axis=0) # Add batch dimension
|
54 |
img = img.astype(np.float32) / 255.0 # Normalize to [0, 1]
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
return img, original_shape
|
57 |
except Exception as e:
|
58 |
raise ValueError(f"Error in pre_processing: {str(e)}")
|
@@ -344,22 +351,23 @@ class ObjectDetectionTool(Tool):
|
|
344 |
# Preprocess the image
|
345 |
img, original_shape = pre_processing(image)
|
346 |
|
347 |
-
# Verify input shape
|
348 |
if len(img.shape) != 4: # Should be NCHW
|
349 |
raise ValueError(f"Invalid input shape: {img.shape}, expected NCHW format")
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
|
|
360 |
|
361 |
# Run inference
|
362 |
-
onnx_input = {self.input_name:
|
363 |
onnx_output = self.onnx_model.run(None, onnx_input)
|
364 |
|
365 |
# Handle shape mismatch by transposing if needed
|
|
|
49 |
|
50 |
# Convert BGR to RGB and normalize
|
51 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # More reliable than array slicing
|
|
|
|
|
52 |
img = img.astype(np.float32) / 255.0 # Normalize to [0, 1]
|
53 |
|
54 |
+
# Convert to NCHW format (batch, channels, height, width)
|
55 |
+
img = np.transpose(img, (2, 0, 1)) # HWC to CHW
|
56 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
57 |
+
|
58 |
+
# Verify final shape
|
59 |
+
if img.shape != (1, 3, 416, 416):
|
60 |
+
print(f"Warning: Final shape is {img.shape}, expected (1, 3, 416, 416)")
|
61 |
+
img = np.reshape(img, (1, 3, 416, 416))
|
62 |
+
|
63 |
return img, original_shape
|
64 |
except Exception as e:
|
65 |
raise ValueError(f"Error in pre_processing: {str(e)}")
|
|
|
351 |
# Preprocess the image
|
352 |
img, original_shape = pre_processing(image)
|
353 |
|
354 |
+
# Verify input shape and convert to NCHW if needed
|
355 |
if len(img.shape) != 4: # Should be NCHW
|
356 |
raise ValueError(f"Invalid input shape: {img.shape}, expected NCHW format")
|
357 |
+
if img.shape[1] != 3: # Should have 3 channels
|
358 |
+
# If channels are last, transpose to NCHW
|
359 |
+
if img.shape[3] == 3:
|
360 |
+
img = np.transpose(img, (0, 3, 1, 2))
|
361 |
+
else:
|
362 |
+
raise ValueError(f"Invalid number of channels: {img.shape[1]}, expected 3")
|
363 |
+
|
364 |
+
# Verify final shape
|
365 |
+
if img.shape != (1, 3, 416, 416):
|
366 |
+
print(f"Warning: Reshaping input from {img.shape} to (1, 3, 416, 416)")
|
367 |
+
img = np.reshape(img, (1, 3, 416, 416))
|
368 |
|
369 |
# Run inference
|
370 |
+
onnx_input = {self.input_name: img}
|
371 |
onnx_output = self.onnx_model.run(None, onnx_input)
|
372 |
|
373 |
# Handle shape mismatch by transposing if needed
|