Upload 3 files
Browse files- best v3(100).pt +3 -0
- main.py +112 -0
- requirements.txt +7 -0
best v3(100).pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b9dc012a5c2e4861c60691272dea39bb065bf709f22182e90c7f90d87ce9619
|
| 3 |
+
size 52054603
|
main.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
import psycopg2
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
model = YOLO('./best v3(100).pt')
|
| 13 |
+
|
| 14 |
+
'''def init_connection():
|
| 15 |
+
return psycopg2.connect(
|
| 16 |
+
host="localhost",
|
| 17 |
+
database="water_meter",
|
| 18 |
+
user="postgres",
|
| 19 |
+
password="16ecr193@kec"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def insert_detection(conn, detection_date, water_meter_column, detected_values_str):
|
| 23 |
+
with conn.cursor() as cur:
|
| 24 |
+
created_time = datetime.now().strftime("%H:%M:%S")
|
| 25 |
+
|
| 26 |
+
cur.execute(f"""
|
| 27 |
+
INSERT INTO water_meter_readings (detection_date, {water_meter_column}, created_time)
|
| 28 |
+
VALUES (%s, %s, %s)
|
| 29 |
+
ON CONFLICT (detection_date) DO UPDATE
|
| 30 |
+
SET {water_meter_column} = EXCLUDED.{water_meter_column},
|
| 31 |
+
created_time = EXCLUDED.created_time
|
| 32 |
+
""", (detection_date, int(detected_values_str), created_time))
|
| 33 |
+
|
| 34 |
+
meter_number = water_meter_column.split('_')[-1]
|
| 35 |
+
difference_column = f'difference_{meter_number}'
|
| 36 |
+
|
| 37 |
+
yesterday = detection_date - timedelta(days=1)
|
| 38 |
+
|
| 39 |
+
cur.execute(f"""
|
| 40 |
+
SELECT {water_meter_column}
|
| 41 |
+
FROM water_meter_readings
|
| 42 |
+
WHERE detection_date = %s
|
| 43 |
+
""", (yesterday,))
|
| 44 |
+
yesterday_result = cur.fetchone()
|
| 45 |
+
|
| 46 |
+
if yesterday_result and yesterday_result[0] is not None:
|
| 47 |
+
difference = int(detected_values_str) - yesterday_result[0]
|
| 48 |
+
else:
|
| 49 |
+
difference = None
|
| 50 |
+
|
| 51 |
+
if difference is not None:
|
| 52 |
+
cur.execute(f"""
|
| 53 |
+
UPDATE water_meter_readings
|
| 54 |
+
SET {difference_column} = %s
|
| 55 |
+
WHERE detection_date = %s
|
| 56 |
+
""", (difference, detection_date))
|
| 57 |
+
else:
|
| 58 |
+
cur.execute(f"""
|
| 59 |
+
UPDATE water_meter_readings
|
| 60 |
+
SET {difference_column} = NULL
|
| 61 |
+
WHERE detection_date = %s
|
| 62 |
+
""", (detection_date,))
|
| 63 |
+
|
| 64 |
+
conn.commit()'''
|
| 65 |
+
|
| 66 |
+
@app.get("/")
|
| 67 |
+
def read_root():
|
| 68 |
+
return {"message": "Welcome to Water Meter Detection API"}
|
| 69 |
+
|
| 70 |
+
@app.post("/detect/")
|
| 71 |
+
async def detect_water_meter(
|
| 72 |
+
#detection_date: str = Form(...),
|
| 73 |
+
#water_meter: int = Form(...),
|
| 74 |
+
file: UploadFile = File(...)
|
| 75 |
+
):
|
| 76 |
+
image_data = await file.read()
|
| 77 |
+
image = Image.open(io.BytesIO(image_data))
|
| 78 |
+
img_array = np.array(image)
|
| 79 |
+
|
| 80 |
+
results = model.predict(source=img_array, conf=0.25, imgsz=640)
|
| 81 |
+
|
| 82 |
+
detected_numbers = []
|
| 83 |
+
for result in results:
|
| 84 |
+
for box in result.boxes:
|
| 85 |
+
label = result.names[box.cls[0].item()]
|
| 86 |
+
x1, _, _, _ = box.xyxy[0].tolist()
|
| 87 |
+
|
| 88 |
+
if label.isdigit():
|
| 89 |
+
detected_numbers.append((x1, label))
|
| 90 |
+
|
| 91 |
+
detected_numbers.sort(key=lambda x: x[0])
|
| 92 |
+
sorted_numbers = [num for _, num in detected_numbers]
|
| 93 |
+
detected_values_str = ''.join(sorted_numbers)
|
| 94 |
+
|
| 95 |
+
'''if detected_values_str:
|
| 96 |
+
water_meter_column = f'water_meter_{water_meter}'
|
| 97 |
+
detection_date_obj = datetime.strptime(detection_date, "%Y-%m-%d").date()
|
| 98 |
+
|
| 99 |
+
conn = init_connection()
|
| 100 |
+
insert_detection(conn, detection_date_obj, water_meter_column, detected_values_str)
|
| 101 |
+
conn.close()'''
|
| 102 |
+
|
| 103 |
+
return JSONResponse(
|
| 104 |
+
content={
|
| 105 |
+
"status": "success",
|
| 106 |
+
"detected_values": detected_values_str if sorted_numbers else "No numeric values detected."
|
| 107 |
+
}
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
import uvicorn
|
| 112 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
ultralytics
|
| 4 |
+
Pillow
|
| 5 |
+
numpy
|
| 6 |
+
psycopg2
|
| 7 |
+
python-multipart
|