Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
| 14 |
# YOLOv5 for digit detection (Pre-trained model)
|
| 15 |
-
yolo_model = YOLO("yolov5s.pt")
|
| 16 |
|
| 17 |
# OCR Models
|
| 18 |
ocr_reader = easyocr.Reader(["en"]) # EasyOCR
|
|
@@ -30,7 +30,14 @@ def enhance_image(image):
|
|
| 30 |
# Detect Digits Using YOLOv5
|
| 31 |
def detect_digits(image):
|
| 32 |
results = yolo_model(image)
|
| 33 |
-
detected_numbers = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return detected_numbers
|
| 35 |
|
| 36 |
# Extract Text Using EasyOCR
|
|
@@ -57,11 +64,11 @@ def process_image(image):
|
|
| 57 |
detected_digits = detect_digits(image)
|
| 58 |
text_easyocr = extract_text_easyocr(enhanced)
|
| 59 |
text_trocr = extract_text_trocr(enhanced)
|
| 60 |
-
|
| 61 |
# Prioritize numeric values from OCR
|
| 62 |
weight_easyocr = extract_weight(text_easyocr)
|
| 63 |
weight_trocr = extract_weight(text_trocr)
|
| 64 |
-
|
| 65 |
final_weight = weight_easyocr if weight_easyocr != "Weight not detected" else weight_trocr
|
| 66 |
return final_weight or "Weight not detected"
|
| 67 |
|
|
|
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
| 14 |
# YOLOv5 for digit detection (Pre-trained model)
|
| 15 |
+
yolo_model = YOLO("yolov5s.pt")
|
| 16 |
|
| 17 |
# OCR Models
|
| 18 |
ocr_reader = easyocr.Reader(["en"]) # EasyOCR
|
|
|
|
| 30 |
# Detect Digits Using YOLOv5
|
| 31 |
def detect_digits(image):
|
| 32 |
results = yolo_model(image)
|
| 33 |
+
detected_numbers = []
|
| 34 |
+
|
| 35 |
+
for result in results:
|
| 36 |
+
if hasattr(result, "boxes"): # Ensure correct format
|
| 37 |
+
for box in result.boxes:
|
| 38 |
+
if box.conf > 0.5: # Confidence threshold
|
| 39 |
+
detected_numbers.append(box.xyxy.tolist())
|
| 40 |
+
|
| 41 |
return detected_numbers
|
| 42 |
|
| 43 |
# Extract Text Using EasyOCR
|
|
|
|
| 64 |
detected_digits = detect_digits(image)
|
| 65 |
text_easyocr = extract_text_easyocr(enhanced)
|
| 66 |
text_trocr = extract_text_trocr(enhanced)
|
| 67 |
+
|
| 68 |
# Prioritize numeric values from OCR
|
| 69 |
weight_easyocr = extract_weight(text_easyocr)
|
| 70 |
weight_trocr = extract_weight(text_trocr)
|
| 71 |
+
|
| 72 |
final_weight = weight_easyocr if weight_easyocr != "Weight not detected" else weight_trocr
|
| 73 |
return final_weight or "Weight not detected"
|
| 74 |
|