muddasser commited on
Commit
78e1fbf
·
verified ·
1 Parent(s): 28c6ec8

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +53 -38
Dockerfile CHANGED
@@ -1,38 +1,53 @@
1
- FROM python:3.10-slim
2
-
3
- # Avoid interactive prompts
4
- ENV DEBIAN_FRONTEND=noninteractive
5
-
6
- # Set environment variables for EasyOCR and Ultralytics
7
- ENV EASYOCR_MODULE_PATH=/app/.EasyOCR
8
- ENV YOLO_CONFIG_DIR=/app/.config/Ultralytics
9
-
10
- # Install system dependencies
11
- RUN apt-get update && apt-get install -y --no-install-recommends \
12
- git curl build-essential ffmpeg libsm6 libxext6 \
13
- && apt-get clean && rm -rf /var/lib/apt/lists/*
14
-
15
- # Set work directory
16
- WORKDIR /app
17
-
18
- # Create directories for EasyOCR and Ultralytics with appropriate permissions
19
- RUN mkdir -p /app/.EasyOCR /app/.config/Ultralytics \
20
- && chmod -R 777 /app/.EasyOCR /app/.config/Ultralytics
21
-
22
- # Copy requirements
23
- COPY requirements.txt .
24
-
25
- # Install Python packages
26
- RUN pip install --no-cache-dir -r requirements.txt
27
-
28
- # Download YOLOv8 ANPR model weights
29
- RUN curl -L -o /app/anpr_yolov8.pt "https://github.com/madalinabuzatu/yolov8-license-plate-detection/releases/download/v1.0/best.pt"
30
-
31
- # Copy app code
32
- COPY app.py .
33
-
34
- # Expose port for Spaces or local run
35
- EXPOSE 7860
36
-
37
- # Run app
38
- CMD ["python", "app.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from ultralytics import YOLO
3
+ import easyocr
4
+ import cv2
5
+ import numpy as np
6
+ import gradio as gr
7
+ import os
8
+
9
+ # Ensure directories exist
10
+ os.makedirs(os.getenv('EASYOCR_MODULE_PATH', '/app/.EasyOCR'), exist_ok=True)
11
+ os.makedirs(os.getenv('YOLO_CONFIG_DIR', '/app/.config/Ultralytics'), exist_ok=True)
12
+
13
+ # Download pretrained ANPR model (trained to detect license plates)
14
+ ANPR_WEIGHTS = "anpr_yolov8.pt"
15
+ if not os.path.exists(ANPR_WEIGHTS):
16
+ os.system(f"wget -O {ANPR_WEIGHTS} https://github.com/madalinabuzatu/yolov8-license-plate-detection/releases/download/v1.0/best.pt")
17
+
18
+ # Load YOLO ANPR model
19
+ model = YOLO(ANPR_WEIGHTS)
20
+
21
+ # Load OCR reader with specified model storage directory
22
+ reader = easyocr.Reader(['en'], model_storage_directory=os.getenv('EASYOCR_MODULE_PATH', '/app/.EasyOCR'))
23
+
24
+ def detect_and_read_plate(image):
25
+ results = model(image)
26
+ for result in results:
27
+ boxes = result.boxes.xyxy.cpu().numpy() # [x1, y1, x2, y2]
28
+ for box in boxes:
29
+ x1, y1, x2, y2 = map(int, box)
30
+ # Crop the detected license plate
31
+ plate_img = image[y1:y2, x1:x2]
32
+ if plate_img.size == 0:
33
+ continue
34
+ # OCR to read text
35
+ ocr_result = reader.readtext(plate_img)
36
+ if ocr_result:
37
+ text = " ".join([res[1] for res in ocr_result])
38
+ print(f"Detected Plate: {text}")
39
+ # Draw bounding box + text
40
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
41
+ cv2.putText(image, text, (x1, y1 - 10),
42
+ cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
43
+
44
+ return image
45
+
46
+ demo = gr.Interface(
47
+ fn=detect_and_read_plate,
48
+ inputs="image",
49
+ outputs="image",
50
+ title="Automatic Number Plate Recognition (ANPR)",
51
+ description="Upload an image of a car to detect and read its license plate."
52
+ )
53
+ demo.launch(server_name="0.0.0.0", server_port=7860)