|
@@ -31,6 +31,8 @@ def init_local_db():
|
|
|
archive_path TEXT,
|
|
archive_path TEXT,
|
|
|
detections TEXT,
|
|
detections TEXT,
|
|
|
summary TEXT,
|
|
summary TEXT,
|
|
|
|
|
+ inference_ms REAL,
|
|
|
|
|
+ raw_array_sample TEXT,
|
|
|
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
|
)
|
|
)
|
|
|
''')
|
|
''')
|
|
@@ -90,7 +92,7 @@ class ModelManager:
|
|
|
"is_health_alert": class_name in ["Abnormal", "Empty_Bunch"],
|
|
"is_health_alert": class_name in ["Abnormal", "Empty_Bunch"],
|
|
|
"box": [float(x1), float(y1), float(x2), float(y2)]
|
|
"box": [float(x1), float(y1), float(x2), float(y2)]
|
|
|
})
|
|
})
|
|
|
- return detections
|
|
|
|
|
|
|
+ return detections, detections_batch[0, :5].tolist()
|
|
|
|
|
|
|
|
def run_pytorch_inference(self, img: Image.Image, conf_threshold: float):
|
|
def run_pytorch_inference(self, img: Image.Image, conf_threshold: float):
|
|
|
results = self.pt_model(img, conf=conf_threshold, verbose=False)
|
|
results = self.pt_model(img, conf=conf_threshold, verbose=False)
|
|
@@ -105,7 +107,7 @@ class ModelManager:
|
|
|
"is_health_alert": class_name in ["Abnormal", "Empty_Bunch"],
|
|
"is_health_alert": class_name in ["Abnormal", "Empty_Bunch"],
|
|
|
"box": box.xyxy.tolist()[0]
|
|
"box": box.xyxy.tolist()[0]
|
|
|
})
|
|
})
|
|
|
- return detections
|
|
|
|
|
|
|
+ return detections, results[0].boxes.data[:5].tolist()
|
|
|
|
|
|
|
|
model_manager = ModelManager(onnx_path='best.onnx', pt_path='best.pt')
|
|
model_manager = ModelManager(onnx_path='best.onnx', pt_path='best.pt')
|
|
|
|
|
|
|
@@ -155,11 +157,15 @@ async def analyze_with_health_metrics(file: UploadFile = File(...), model_type:
|
|
|
image_bytes = await file.read()
|
|
image_bytes = await file.read()
|
|
|
img = Image.open(io.BytesIO(image_bytes))
|
|
img = Image.open(io.BytesIO(image_bytes))
|
|
|
|
|
|
|
|
|
|
+ import time
|
|
|
|
|
+ start_time = time.perf_counter()
|
|
|
# Select Inference Engine
|
|
# Select Inference Engine
|
|
|
if model_type == "pytorch":
|
|
if model_type == "pytorch":
|
|
|
- detections = model_manager.run_pytorch_inference(img, current_conf)
|
|
|
|
|
|
|
+ detections, raw_sample = model_manager.run_pytorch_inference(img, current_conf)
|
|
|
else:
|
|
else:
|
|
|
- detections = model_manager.run_onnx_inference(img, current_conf)
|
|
|
|
|
|
|
+ detections, raw_sample = model_manager.run_onnx_inference(img, current_conf)
|
|
|
|
|
+ end_time = time.perf_counter()
|
|
|
|
|
+ inference_ms = (end_time - start_time) * 1000
|
|
|
|
|
|
|
|
# Initialize summary
|
|
# Initialize summary
|
|
|
summary = {name: 0 for name in model_manager.class_names.values()}
|
|
summary = {name: 0 for name in model_manager.class_names.values()}
|
|
@@ -178,8 +184,8 @@ async def analyze_with_health_metrics(file: UploadFile = File(...), model_type:
|
|
|
# Save to SQLite
|
|
# Save to SQLite
|
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn = sqlite3.connect(DB_PATH)
|
|
|
cursor = conn.cursor()
|
|
cursor = conn.cursor()
|
|
|
- cursor.execute("INSERT INTO history (filename, archive_path, detections, summary) VALUES (?, ?, ?, ?)",
|
|
|
|
|
- (file.filename, archive_path, json.dumps(detections), json.dumps(summary)))
|
|
|
|
|
|
|
+ cursor.execute("INSERT INTO history (filename, archive_path, detections, summary, inference_ms, raw_array_sample) VALUES (?, ?, ?, ?, ?, ?)",
|
|
|
|
|
+ (file.filename, archive_path, json.dumps(detections), json.dumps(summary), inference_ms, json.dumps(raw_sample)))
|
|
|
conn.commit()
|
|
conn.commit()
|
|
|
conn.close()
|
|
conn.close()
|
|
|
|
|
|
|
@@ -189,6 +195,8 @@ async def analyze_with_health_metrics(file: UploadFile = File(...), model_type:
|
|
|
"total_count": len(detections),
|
|
"total_count": len(detections),
|
|
|
"industrial_summary": summary,
|
|
"industrial_summary": summary,
|
|
|
"detections": detections,
|
|
"detections": detections,
|
|
|
|
|
+ "inference_ms": inference_ms,
|
|
|
|
|
+ "raw_array_sample": raw_sample,
|
|
|
"archive_id": unique_id
|
|
"archive_id": unique_id
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -240,18 +248,22 @@ async def process_batch(files: List[UploadFile] = File(...), model_type: str = F
|
|
|
shutil.copyfileobj(file.file, f_out)
|
|
shutil.copyfileobj(file.file, f_out)
|
|
|
temp_files.append(path)
|
|
temp_files.append(path)
|
|
|
|
|
|
|
|
|
|
+ import time
|
|
|
# 2. Detect
|
|
# 2. Detect
|
|
|
img = Image.open(path)
|
|
img = Image.open(path)
|
|
|
- if model_type == "pytorch":
|
|
|
|
|
- detections = model_manager.run_pytorch_inference(img, current_conf)
|
|
|
|
|
- else:
|
|
|
|
|
- detections = model_manager.run_onnx_inference(img, current_conf)
|
|
|
|
|
|
|
+ # FORCE PYTORCH for Batch
|
|
|
|
|
+ start_time = time.perf_counter()
|
|
|
|
|
+ detections, raw_sample = model_manager.run_pytorch_inference(img, current_conf)
|
|
|
|
|
+ end_time = time.perf_counter()
|
|
|
|
|
+ inference_ms = (end_time - start_time) * 1000
|
|
|
|
|
|
|
|
# 3. Process all detections in the image
|
|
# 3. Process all detections in the image
|
|
|
for det in detections:
|
|
for det in detections:
|
|
|
batch_results.append({
|
|
batch_results.append({
|
|
|
"path": path,
|
|
"path": path,
|
|
|
- "yolo": det
|
|
|
|
|
|
|
+ "yolo": det,
|
|
|
|
|
+ "inference_ms": inference_ms,
|
|
|
|
|
+ "raw_array_sample": raw_sample
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@@ -269,7 +281,9 @@ async def process_batch(files: List[UploadFile] = File(...), model_type: str = F
|
|
|
for item in batch_results:
|
|
for item in batch_results:
|
|
|
detailed_detections.append({
|
|
detailed_detections.append({
|
|
|
"filename": os.path.basename(item['path']),
|
|
"filename": os.path.basename(item['path']),
|
|
|
- "detection": item['yolo']
|
|
|
|
|
|
|
+ "detection": item['yolo'],
|
|
|
|
|
+ "inference_ms": item['inference_ms'],
|
|
|
|
|
+ "raw_array_sample": item['raw_array_sample']
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
try:
|
|
try:
|