Update app.py
Browse files
app.py
CHANGED
@@ -52,19 +52,58 @@ class SmartGlassesSystem:
|
|
52 |
device=0 if using_gpu else -1
|
53 |
)
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
"object
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
logger.info("All models loaded successfully!")
|
64 |
|
65 |
except Exception as e:
|
66 |
logger.error(f"Error initializing models: {str(e)}")
|
67 |
raise RuntimeError(f"Failed to initialize AI models: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
def get_ocr_reader(self, language_choice):
|
70 |
"""Get or create appropriate OCR reader based on language choice"""
|
|
|
52 |
device=0 if using_gpu else -1
|
53 |
)
|
54 |
|
55 |
+
# Check if timm is installed for object detection
|
56 |
+
try:
|
57 |
+
import timm
|
58 |
+
logger.info("Loading object detection model...")
|
59 |
+
self.detector = pipeline(
|
60 |
+
"object-detection",
|
61 |
+
model="facebook/detr-resnet-50",
|
62 |
+
device=0 if using_gpu else -1
|
63 |
+
)
|
64 |
+
except ImportError:
|
65 |
+
logger.warning("timm library not found, using YOLOv5 as fallback for object detection")
|
66 |
+
try:
|
67 |
+
import torch
|
68 |
+
# Use YOLOv5 as a fallback (it has fewer dependencies)
|
69 |
+
self.detector = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
70 |
+
# Make detector interface compatible with transformers pipeline
|
71 |
+
self._original_detect = self.detector
|
72 |
+
self.detector = self._yolo_detector_wrapper
|
73 |
+
except Exception as e2:
|
74 |
+
logger.error(f"Fallback object detection also failed: {str(e2)}")
|
75 |
+
logger.warning("Object detection will be disabled")
|
76 |
+
self.detector = self._dummy_detector
|
77 |
|
78 |
logger.info("All models loaded successfully!")
|
79 |
|
80 |
except Exception as e:
|
81 |
logger.error(f"Error initializing models: {str(e)}")
|
82 |
raise RuntimeError(f"Failed to initialize AI models: {str(e)}")
|
83 |
+
|
84 |
+
def _yolo_detector_wrapper(self, image):
|
85 |
+
"""Wrapper to make YOLOv5 output compatible with transformers pipeline format"""
|
86 |
+
results = self._original_detect(image)
|
87 |
+
detections = []
|
88 |
+
|
89 |
+
# Convert YOLOv5 results to transformers pipeline format
|
90 |
+
for i, (x1, y1, x2, y2, conf, cls) in enumerate(results.xyxy[0]):
|
91 |
+
detections.append({
|
92 |
+
'score': float(conf),
|
93 |
+
'label': results.names[int(cls)],
|
94 |
+
'box': {
|
95 |
+
'xmin': int(x1),
|
96 |
+
'ymin': int(y1),
|
97 |
+
'xmax': int(x2),
|
98 |
+
'ymax': int(y2)
|
99 |
+
}
|
100 |
+
})
|
101 |
+
return detections
|
102 |
+
|
103 |
+
def _dummy_detector(self, image):
|
104 |
+
"""Dummy detector when no object detection is available"""
|
105 |
+
logger.warning("Object detection is disabled due to missing dependencies")
|
106 |
+
return []
|
107 |
|
108 |
def get_ocr_reader(self, language_choice):
|
109 |
"""Get or create appropriate OCR reader based on language choice"""
|