import numpy as np
import time
import cv2

# Arguments construction
args={
"prototxt":"MobileNetSSD_deploy.prototxt.txt",
"model":"MobileNetSSD_deploy.caffemodel",
"confidence":0.2,
}

# ModelNet SSD Object list init
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle",
"bus", "car", "cat", "chair", "cow", "diningtable" , " dog",
"horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "monitor"]

COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

# Load model file
print("Load Neural Network...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])

print("Start Camera...")
camera = cv2.VideoCapture(0)
time.sleep(2.0)

prev_time = time.time()
fps = 0

while True:
    # Get video sttream. max width 800 pixels 
    success, frame = camera.read()
    if not success:
        break
    else:
        ret, frame=camera.read() #from video or ip cam
        # frame = imutils.resize(frame, width=800)
        
        # Create blob from image
        (h, w) = frame.shape[:2]
        blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5)

        # Feed input to neural network 
        net.setInput(blob)
        detections = net.forward()

        # Detection loop
        for i in np.arange(0, detections.shape[2]):
            # Compute Object detection probability
            confidence = detections[0, 0, i, 2]
            
            # Suppress low probability
            if confidence > args["confidence"]:
                # Get index and position of detected object
                idx = int(detections[0, 0, i, 1])
                box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                (startX, startY, endX, endY) = box.astype("int")

                # Create box and label
                label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
                cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2)
                
                y = startY - 15 if startY - 15 > 15 else startY + 15
                cv2.putText(frame, label, (startX, y),cv2.FONT_HERSHEY_DUPLEX, 0.5, COLORS[idx], 2)
        
        # Calculate FPS
        curr_time = time.time()
        fps = 1 / (curr_time - prev_time)
        prev_time = curr_time
        # Draw a white background rectangle for the FPS text
        cv2.rectangle(frame, (frame.shape[1] - 150, 5), (frame.shape[1], 35), (255, 255, 255), -1)
        # Display FPS on the frame in the top-right corner
        cv2.putText(frame, f"FPS: {int(fps)}", (frame.shape[1] - 150, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)

        cv2.imshow('Object Detection', frame)
            
        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

camera.release()
cv2.destroyAllWindows()
