weapon-detection / demos /single_image_inference.py
ishworrsubedii's picture
feat: add complete pipeline and Streamlit code This commit introduces a complete pipeline for both single and real-time inferences using cameras. It includes the implementation of Streamlit code to facilitate the process.
c640bc9 verified
raw
history blame
1.17 kB
"""
Created By: ishwor subedi
Date: 2024-04-04
"""
from services.weapon_det_service.weapon_detection_service import DetectionService
import cv2 as cv
def single_image_inference(image_path):
detection_service = DetectionService(
model_path='resources/models/v1/best.pt',
)
results = detection_service.image_det_save(
image_path=image_path,
thresh=0.2
)
for result in results:
original_image = result.orig_img
bbox = result.boxes.xyxy.int().tolist()
for i, bbox in enumerate(bbox):
x1, y1, x2, y2 = bbox
cv.putText(original_image, f'{result.names[result.boxes.cls[i].int().tolist()]}', (x1, y1 - 10),
cv.FONT_HERSHEY_SIMPLEX,
0.9,
(0, 0, 255), 2)
cv.rectangle(original_image, (x1, y1), (x2, y2), (0, 0, 255), 2)
return original_image
if __name__ == '__main__':
image_path = '/home/ishwor/Desktop/gun-detection/images/cam_images/th-3711382641.jpg'
image = single_image_inference(image_path)
cv.imshow('Weapon Detection', image)
cv.waitKey(0)
cv.destroyAllWindows()