I tried to implement it for a region of interest but gave me this error.
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import imutils
import time
import cv2
from color_recognition_api import color_histogram_feature_extraction
from color_recognition_api import knn_classifier
import os
import os.path
# global x1 , y1 , w1, h1, endX, startX, endY, startY , area1
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe('prototxt.txt', 'caffemodel')
print("[INFO] starting video stream...")
prediction = 'n.a.'
# checking whether the training data is ready
PATH = './training.data'
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
print ('training data is ready, classifier is loading...')
else:
print ('training data is being created...')
open('training.data', 'w')
color_histogram_feature_extraction.training()
print ('training data is ready, classifier is loading...')
vs = cv2.VideoCapture(0)
# vs.release()
# vs = cv2.VideoCapture(0)
time.sleep(2.0)
fps = FPS().start()
width = vs.get(3) # float
height = vs.get(4) # float
print width, height
fps = FPS().start()
while True:
ret, frame = vs.read()
# frame = imutils.resize(frame, width=400)
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
big_area = 0
big_center = 320
detected = 0
for i in np.arange(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
object_type = int(detections[0, 0, i, 1])
if object_type == 15 and confidence > 0.2:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
label = "{}: {:.2f}%".format('person', confidence * 100)
cv2.rectangle(frame, (startX, startY), (endX, endY), [0, 0, 255], 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 0, 255], 2)
# ROI = green[startX:endX, startY:endY]
ROI1 = frame[startX:endX, startY:endY]
color_histogram_feature_extraction.color_histogram_of_test_image(ROI1)
prediction = knn_classifier.main('training.data', 'test.data')
print prediction
rect_area = (endX-startX)*(endY-startY)
# print (endX, startX, endY, startY, type(endX),type(startX),type(endY),type(startY))
detected = 1
if rect_area > big_area:
big_area = rect_area
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
fps.update()
fps.stop()
vs.release()
cv2.destroyAllWindows()