Exemple #1
0
 def get_frame(self):
     count = 0
     _, frame = self.video.read()
     height, width = frame.shape[:2]
     if count == 0:
         frame, boxes, confidences, classids, idxs = infer_image(
             net, layer_names, height, width, frame, colors, labels, FLAGS)
         count += 1
     else:
         frame, boxes, confidences, classids, idxs = infer_image(
             net,
             layer_names,
             height,
             width,
             frame,
             colors,
             labels,
             FLAGS,
             boxes,
             confidences,
             classids,
             idxs,
             infer=False)
         count = (count + 1) % 6
     ret, jpeg = cv.imencode('.jpg', frame)
     return jpeg.tobytes()
Exemple #2
0
    def infer(self, vs, net, args, layer_names, colors, labels, FLAGS):
        """
        Pass video stream to model and infer car ready signals
        """
        count = 0
        fps = FPS().start()

        end = time.time() + CV_INFER_SECONDS

        output = {}

        while time.time() < end:

            frame = vs.read()
            height, width = frame.shape[:2]

            if count == 0:
                frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
                                    height, width, frame, colors, labels, FLAGS)
                count += 1
            else:
                frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
                                    height, width, frame, colors, labels, FLAGS, boxes, confidences, classids, idxs, infer=False)
                count = (count + 1) % 6

            cv2.imshow('webcam', frame)

            print(classids, idxs)

            #output[datetime.now()] = [classids[idxs], confidences[idxs]]

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
                
			# update the FPS counter
            fps.update()                

        # stop the timer and display FPS information
        fps.stop()
        print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
        print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

        # do a bit of cleanup
        print('clean environment: destroy windows')
        cv2.destroyAllWindows()

        output = {'time' : ['person', 100]}

        return (True, output)
Exemple #3
0
def photo_labels(path):

    FLAGS = {}
    FLAGS['labels'] = './yolov3-coco/coco-labels'
    # Download the YOLOv3 models if needed

    # Get the labels
    labels = open(FLAGS['labels']).read().strip().split('\n')
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')
    FLAGS['config'] = './yolov3-coco/yolov3.cfg'
    FLAGS['weights'] = './yolov3-coco/yolov3.weights'
    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(FLAGS['config'], FLAGS['weights'])

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]
    img = cv.imread(path)
    height, width = img.shape[:2]
    img, _, _, classids, idxs, text = infer_image(net, layer_names, height,
                                                  width, img, colors, labels,
                                                  FLAGS)

    #print(text)
    return text
def test_object_improved(path_in, path_out, suffix = 'object_improved'):
	parser = argparse.ArgumentParser()
	FLAGS, unparsed = parser.parse_known_args()

	FLAGS.model_path = '../yolov3-coco/'
	FLAGS.weights = '../yolov3-coco/yolov3-spp.weights'
	FLAGS.config = '../yolov3-coco/yolov3-spp.cfg'
	FLAGS.video_path = path_in
	FLAGS.video_output_path = f'{path_out}_{suffix}.avi'
	FLAGS.labels = '../yolov3-coco/coco-labels'
	FLAGS.confidence = 0.1
	FLAGS.threshold = 0.3
	FLAGS.download_model = False
	FLAGS.show_time = False

	vid = cv.VideoCapture(FLAGS.video_path)
	height, width, writer = None, None, None

	labels = open(FLAGS.labels).read().strip().split('\n')
	colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

	net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)
	layer_names = net.getLayerNames()
	layer_names = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

	print(f'starting {suffix}')
	time_0 = time.time()

	frame_number = 0
	while True:
		grabbed, frame = vid.read()

		if not grabbed:
			break
		else:
			frame_number += 1

		if width is None or height is None:
			height, width = frame.shape[:2]

		img, boxes, confidences, classids, idxs = infer_image(net, layer_names, height, width, frame, colors, labels, FLAGS)

		output_array = []
		for index in range(len(classids)):
			output_array.append({'name' : labels[classids[index]], 'percentage_probability' : confidences[index] * 100})

		pfh.per_frame_handler(frame_number, output_array, suffix = suffix)

		if writer is None:
			fourcc = cv.VideoWriter_fourcc(*"MJPG")
			writer = cv.VideoWriter(FLAGS.video_output_path, fourcc, 30, (frame.shape[1], frame.shape[0]), True)

		writer.write(frame)

	writer.release()
	vid.release()

	print(f'mode {suffix} finished, elapsed time : {time.time() - time_0}s')
Exemple #5
0
def classify(frame_bgr):

	global count

	frame = imutils.resize(frame_bgr, width=600)


	height, width = frame.shape[:2]

	boxes, confidences, classids = [],[],[]

	results = []


	frame_out, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
						height, width, frame, colors, labels, FLAGS)


	height, width = frame.shape[:2]

	if count == 0:
		frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
							height, width, frame, colors, labels, FLAGS)
		count += 1
	else:
		frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
							height, width, frame, colors, labels, FLAGS, boxes, confidences, classids, idxs, infer=False)
		count = (count + 1) % 6

	print_categories(boxes, confidences, classids, labels)

	results = []


	for i in range(len(classids)):
		results.append((labels[classids[i]], confidences[i], boxes[i]))

	frame_out = frame.copy()

	return frame_out, results
Exemple #6
0
def yolo_detect(frames,labelh,net):
    parser = argparse.ArgumentParser()
    
    parser.add_argument('-l', '--labels',
                        type=str,
                        default='./yolov3-coco/coco-labels',
                        help='Path to the file having the labels in a new-line seperated way.')

    parser.add_argument('-c', '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
				probabiity less than the confidence value. \
				default: 0.5')

    parser.add_argument('-th', '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the Non-Max Suppresion')                                     

    FLAGS, unparsed = parser.parse_known_args()
    
  
    # Get the labels
    labels = open(FLAGS.labels).read().strip().split('\n')
    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')
    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    
    layer_names = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
   
    height , width =  None, None
    writer = None
    count = 0
    for frame in frames:
        print("frame count :",count)
        detect = 0
        if(count%2==0):
            if width is None or height is None:
                width = frame.shape[1]
                height  = frame.shape[0]
            
            detect = infer_image(net, layer_names, height, width, frame, colors, labels, FLAGS,labelh)
        count += 1
        if detect == 0:
            continue
        else:
            return detect
    return detect
def detectobject(frame):
    print("entered")
    confidence = 0.5
    threshold = 0.3
    height, width = frame.shape[:2]
    objects = []
    confidences, classids, idxs = infer_image(net, layer_names, height, width,
                                              frame, labels, confidence,
                                              threshold)
    if len(idxs) > 0:
        for i in idxs.flatten():
            if (labels[classids[i]] not in objects):
                objects.append(labels[classids[i]])
    print("[INFO] Cleaning up...")

    return objects
    def predict(self, in_path, out_img_path, out_json_path):
        # Read the image
        try:
            print('read image from %s' % in_path)
            img = cv.imread(in_path)
            height, width = img.shape[:2]
        except:
            raise 'Image cannot be loaded!\n\
                               Please check the path provided!'

        finally:
            img, json_data = infer_image(self.net, self.layer_names, height,
                                         width, img, self.colors, self.labels,
                                         self.FLAGS)
            cv.imwrite(out_img_path, img)
            with open(out_json_path, "w") as json_file:
                json.dump(json_data, json_file)
def detectobjects(frame,
                  weights=os.path.join(BASE_URL, 'yolov3.weights'),
                  config=os.path.join(BASE_URL, 'yolov3.cfg'),
                  labels=os.path.join(BASE_URL, 'coco-labels')):
    print("entered")
    confidence = 0.5
    threshold = 0.3

    labels = open(labels).read().strip().split("\n")
    net = cv.dnn.readNetFromDarknet(config, weights)

    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    frame_count = 0
    # while True:
    # grabbed, frame = vid.read()

    # print(frame_count)
    frame_count = frame_count + 1

    # if width is None or height is None:
    height, width = frame.shape[:2]
    objects = []
    confidences, classids, idxs = infer_image(net, layer_names, height, width,
                                              frame, labels, confidence,
                                              threshold)

    if len(idxs) > 0:
        for i in idxs.flatten():
            if (labels[classids[i]] not in objects):
                objects.append(labels[classids[i]])

    print("[INFO] Cleaning up...")

    return objects
def yolo():
    import numpy as np
    import argparse
    import cv2 as cv
    import subprocess
    import time
    import os

    from yolo_utils import infer_image, show_image
    engine = pyttsx3.init()
    engine.say("Welcome to the Navigation assistant.")
    engine.runAndWait()

    FLAGS = []
    parser = argparse.ArgumentParser()

    parser.add_argument('-m',
                        '--model-path',
                        type=str,
                        default='./yolov3-coco/',
                        help='The directory where the model weights and \
			  configuration files are.')

    parser.add_argument('-w',
                        '--weights',
                        type=str,
                        default='./yolov3-coco/yolov3.weights',
                        help='Path to the file which contains the weights \
			 	for YOLOv3.')

    parser.add_argument(
        '-cfg',
        '--config',
        type=str,
        default='./yolov3-coco/yolov3.cfg',
        help='Path to the configuration file for the YOLOv3 model.')

    parser.add_argument('-i',
                        '--image-path',
                        type=str,
                        help='The path to the image file')

    parser.add_argument('-v',
                        '--video-path',
                        type=str,
                        help='The path to the video file')

    parser.add_argument('-vo',
                        '--video-output-path',
                        type=str,
                        default='./output.avi',
                        help='The path of the output video file')

    parser.add_argument('-l',
                        '--labels',
                        type=str,
                        default='./yolov3-coco/coco-labels',
                        help='Path to the file having the \
					labels in a new-line seperated way.')

    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
				probabiity less than the confidence value. \
				default: 0.5')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
				Non-Max Suppresion')

    parser.add_argument(
        '--download-model',
        type=bool,
        default=False,
        help='Set to True, if the model weights and configurations \
				are not present on your local machine.')

    parser.add_argument('-t',
                        '--show-time',
                        type=bool,
                        default=False,
                        help='Show the time taken to infer each image.')

    FLAGS, unparsed = parser.parse_known_args()

    # Download the YOLOv3 models if needed
    if FLAGS.download_model:
        subprocess.call(['./yolov3-coco/get_model.sh'])

# Get the labels
    labels = open(FLAGS.labels).read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    # If both image and video files are given then raise error
    if FLAGS.image_path is None and FLAGS.video_path is None:
        print('Neither path to an image or path to video provided')
        print('Starting Inference on Webcam')
        count = 0
        vid = cv.VideoCapture(0)
        while True:
            _, frame = vid.read()
            height, width = frame.shape[:2]
            if count == 0:
                frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
            height, width, frame, colors, labels, FLAGS)
                count += 1
            else:
                frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
            height, width, frame, colors, labels, FLAGS, boxes, confidences, classids, idxs, infer=False)
                count = (count + 1) % 6
                cv.imshow('webcam', frame)

            if cv.waitKey(1) & 0xFF == ord('q'):
                break
        vid.release()
        cv.destroyAllWindows()
Exemple #11
0
def detectionofkiranastore(
):  #function to get detected class id and conture values
    parser = argparse.ArgumentParser()

    parser.add_argument('-m',
                        '--model-path',
                        type=str,
                        default='./yolov3-coco/',
                        help='The directory where the model weights and \
			  configuration files are.')

    parser.add_argument('-w',
                        '--weights',
                        type=str,
                        default='./yolov3-coco/yolov3.weights',
                        help='Path to the file which contains the weights \
			 	for YOLOv3.')

    parser.add_argument(
        '-cfg',
        '--config',
        type=str,
        default='./yolov3-coco/yolov3.cfg',
        help='Path to the configuration file for the YOLOv3 model.')

    parser.add_argument('-l',
                        '--labels',
                        type=str,
                        default='./yolov3-coco/coco-labels',
                        help='Path to the file having the \
					labels in a new-line seperated way.')

    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
				probabiity less than the confidence value. \
				default: 0.5')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
				Non-Max Suppresion')

    FLAGS, unparsed = parser.parse_known_args()

    # Download the YOLOv3 models if needed
    # Get the labels
    labels = open(FLAGS.labels).read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]
    count = 0
    # Read the image
    try:
        img = cv.imread('item.jpg')  #read the object shown
        height, width = img.shape[:2]
    except:
        raise 'Image cannot be loaded!\n\
							Please check the path provided!'

    finally:
        img, _, _, _, _ = infer_image(net, layer_names, height, width, img,
                                      colors, labels, FLAGS)
        if count == 0:
            frame, boxes, confidences, classids, idxs = infer_image(
                net, layer_names, height, width, img, colors, labels, FLAGS)
            count += 1
        else:
            frame, boxes, confidences, classids, idxs = infer_image(
                net,
                layer_names,
                height,
                width,
                img,
                colors,
                labels,
                FLAGS,
                boxes,
                confidences,
                classids,
                idxs,
                infer=False)
            count = (count + 1) % 6
    return classids, boxes
Exemple #12
0
def yolo():
    parser = argparse.ArgumentParser()

    parser.add_argument('-m',
                        '--model-path',
                        type=str,
                        default='./yolov3-coco/',
                        help='The directory where the model weights and \
        configuration files are.')

    parser.add_argument('-w',
                        '--weights',
                        type=str,
                        default='./yolov3-coco/yolov3.weights',
                        help='Path to the file which contains the weights \
        for YOLOv3.')

    parser.add_argument(
        '-cfg',
        '--config',
        type=str,
        default='./yolov3-coco/yolov3.cfg',
        help='Path to the configuration file for the YOLOv3 model.')

    parser.add_argument('-vo',
                        '--video-output-path',
                        type=str,
                        default='./output.avi',
                        help='The path of the output video file')

    parser.add_argument('-l',
                        '--labels',
                        type=str,
                        default='./yolov3-coco/coco-labels',
                        help='Path to the file having the \
          labels in a new-line seperated way.')

    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
        probabiity less than the confidence value. \
        default: 0.5')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
        Non-Max Suppresion')

    parser.add_argument(
        '--download-model',
        type=bool,
        default=False,
        help='Set to True, if the model weights and configurations \
        are not present on your local machine.')

    parser.add_argument('-t',
                        '--show-time',
                        type=bool,
                        default=False,
                        help='Show the time taken to infer each image.')

    FLAGS, unparsed = parser.parse_known_args()
    #print(FLAGS)

    # Get the labels
    labels = open(FLAGS.labels).read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    ################################

    height, width = frame.shape[:2]

    img, bboxes, _, classid, _ = infer_image(net, layer_names, height, width,
                                             frame, colors, labels, FLAGS)

    global boxes
    boxes = []  #It's a list now

    j = 0
    for i in classid:
        if i == 0:
            print("persons bounding box is: ", bboxes[j])
            boxes.append(bboxes[j].copy())
            print(boxes[i])
        j = j + 1

    ############################temp ###########33
    #for index,value in enumerate(boxes):
    itr = 0
    for i in range(len(boxes)):
        itr = itr + 1
        name = 'dataset/' + str("person") + str(itr) + ".jpg"
        y = boxes[i][1]
        x = boxes[i][0]
        h = boxes[i][3]
        w = boxes[i][2]
        crop_img = img[y:y + h, x:x + w]
        cv.imwrite(name, crop_img)

        detector = MTCNN()
        print("I am a detector phewww !")
        print(detector.detect_faces(crop_img))
        face_cropped = detector.detect_faces(crop_img)

        print(face_cropped, "Debug")

        if (len(face_cropped) > 0):
            boxes_face = (face_cropped[0]['box'])
            y1 = boxes_face[1]
            x1 = boxes_face[0]
            h1 = boxes_face[3]
            w1 = boxes_face[2]
            crop_img_2 = crop_img[y1:y1 + h1, x1:x1 + w1]
            name = 'dataset/' + str("face") + str(itr) + '.jpg'
            cv.imwrite(name, crop_img_2)

        #crop_img_2 = cv2.resize(crop_img_2,(100,100),interpolation=cv2.INTER_AREA)

        rec = FaceID()

        # Matching Part
        images = []
        for img in glob.glob("dataset/face*.jpg"):

            n = cv2.imread(img)
            images.append(n)

        #for img in images:
        # img = cv2.resize(img,(100,100),interpolation=cv2.INTER_AREA)
        #if(np.linalg.norm(img-crop_img_2)>=0.9):
        # val = np.linalg.norm(img-crop_img_2)
        #print("Amir won",val)

        # Matching Part End

    ##########################temp done#########33

    my_tuple = []

    for i in bboxes:
        my_tuple.append(tuple(i))

    #print(my_tuple)

    # Create MultiTracker object
    multiTracker = cv2.MultiTracker_create()

    # Initialize MultiTracker
    colors_multi = []
    for bbox in my_tuple:
        multiTracker.add(createTrackerByName(trackerType), frame, bbox)
        colors_multi.append((randint(64, 255), randint(64,
                                                       255), randint(64, 255)))

    return multiTracker, colors_multi
        frameCount = 0
        
        while True:
            grabbed, frame = vid.read()
            print("Frame count",frameCount)

            # Checking if the complete video is read
            if not grabbed:
                break

            if width is None or height is None:
                height , width = frame.shape[:2]
            #Take first frame from each second for detection  

            if(frameCount%1==0):
                frame, detect = infer_image(net, layer_names, height, width, frame, colors, labels, FLAGS, frameCount)
            if writer is None:
                # Initialize the video writer
                fourcc = cv.VideoWriter_fourcc(*"MJPG")
                writer = cv.VideoWriter(FLAGS.video_output_path, fourcc, fps,(frame.shape[1], frame.shape[0]), True)
            writer.write(frame)
                    #Check the frame contain any detection, if detection is occur, label statutory warning on the next 120 frames
            if(detect==1):
                for i in range(1,10):
                    grabbed,frame = vid.read()
                    frameCount += 1
                    print("Frame count",frameCount)
                    height, width = frame.shape[:2]
                    add_label(frame,height,'smoke.png')
                    labelledImg = cv.imread("pasted_image.jpg")
                    if writer is None:
Exemple #14
0
def gen():
    parser = argparse.ArgumentParser()
    #############default parameters for testing pourpose##############

    parser.add_argument('-m',
                        '--model-path',
                        type=str,
                        default='./yolov3-coco/',
                        help='The directory where the model weights and \
			  configuration files are.')

    parser.add_argument('-w',
                        '--weights',
                        type=str,
                        default='./yolov3-coco/yolov3.weights',
                        help='Path to the file which contains the weights \
			 	for YOLOv3.')

    parser.add_argument(
        '-cfg',
        '--config',
        type=str,
        default='./yolov3-coco/yolov3.cfg',
        help='Path to the configuration file for the YOLOv3 model.')

    parser.add_argument('-vo',
                        '--video-output-path',
                        type=str,
                        default='./output.avi',
                        help='The path of the output video file')

    parser.add_argument('-l',
                        '--labels',
                        type=str,
                        default='./yolov3-coco/coco-labels',
                        help='Path to the file having the \
					labels in a new-line seperated way.')

    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
				probabiity less than the confidence value. \
				default: 0.5')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
				Non-Max Suppresion')

    FLAGS, unparsed = parser.parse_known_args()
    #############################################################################

    # Get the labels
    labels = open(FLAGS.labels).read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    # Infer real-time from webcam to webapp
    count = 0

    vid = cv.VideoCapture(
        0
    )  #change 0 for webcam 1 for external camera but mounted to edge server
    #vid=cv.VideoCapture(ip address)for remoe camera connected to server
    while True:
        _, frame = vid.read()
        height, width = frame.shape[:2]

        if count == 0:
            frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
                 height, width, frame, colors, labels, FLAGS)
            count += 1

        else:
            frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
                 height, width, frame, colors, labels, FLAGS, boxes, confidences, classids, idxs, infer=False)
            count = (count + 1) % 6
        cv.imwrite('item.jpg', frame)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' +
               open('item.jpg', 'rb').read() + b'\r\n')
def imag_det(img_path, obj_dec):
    parser = argparse.ArgumentParser()
    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
                probabiity less than the confidence value. \
                default: 0.5')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
                Non-Max Suppresion')

    parser.add_argument(
        '--download-model',
        type=bool,
        default=False,
        help='Set to True, if the model weights and configurations \
                are not present on your local machine.')

    parser.add_argument('-t',
                        '--show-time',
                        type=bool,
                        default=False,
                        help='Show the time taken to infer each image.')

    FLAGS, unparsed = parser.parse_known_args()

    labels = open('darknet\\data\\obj.names').read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(
        'darknet/cfg/yolov3_custom.cfg',
        'darknet/backup/yolov3_custom_last.weights')

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]
    labels = open('darknet\\data\\obj.names').read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(
        'darknet/cfg/yolov3_custom.cfg',
        'darknet/backup/yolov3_custom_last.weights')

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    try:
        img = cv.imread(img_path)
        height, width = img.shape[:2]
    except:
        raise 'Image cannot be loaded!\n\
                            Please check the path provided!'

    finally:
        img, _, _, _, _ = infer_image(net, obj_dec, layer_names, height, width,
                                      img, colors, labels, FLAGS)

        cv.imwrite('image_out.jpg', img)
 def infer(self, frame):
     self.frame, _, _, _, _ = infer_image(self.net, self.layer_names, self.height, self.width, frame, self.colors, self.labels)
     return self.frame
def live_feed(obj_dec):
    parser = argparse.ArgumentParser()
    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
                probabiity less than the confidence value. \
                default: 0.5')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
                Non-Max Suppresion')

    parser.add_argument(
        '--download-model',
        type=bool,
        default=False,
        help='Set to True, if the model weights and configurations \
                are not present on your local machine.')

    parser.add_argument('-t',
                        '--show-time',
                        type=bool,
                        default=False,
                        help='Show the time taken to infer each image.')

    FLAGS, unparsed = parser.parse_known_args()

    labels = open('darknet\\data\\coco.names').read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet('darknet\\cfg\\yolov3.cfg',
                                    'darknet\\yolov3.weights')

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    #net = cv.dnn.readNetFromDarknet('darknet\\cfg/yolov3_custom.cfg', 'darknet/yolov3.weights')
    count = 0
    vid = cv.VideoCapture(0)
    while True:
        _, frame = vid.read()
        height, width = frame.shape[:2]

        if count == 0:
            frame, boxes, confidences, classids, idxs = infer_image(net,  obj_dec, layer_names, \
                                height, width, frame, colors, labels, FLAGS)
            count += 1
        else:
            frame, boxes, confidences, classids, idxs = infer_image(net,  obj_dec, layer_names, \
                                height, width, frame, colors, labels, FLAGS, boxes, confidences, classids, idxs, infer=False)
            count = (count + 1) % 6

        cv.imshow('webcam', frame)

        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    vid.release()
    cv.destroyAllWindows()
def vid_det(vid_path, obj_dec):
    parser = argparse.ArgumentParser()
    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
                probabiity less than the confidence value. \
                default: 0.5')

    parser.add_argument('-vo',
                        '--video-output-path',
                        type=str,
                        default='./output.avi',
                        help='The path of the output video file')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
                Non-Max Suppresion')

    parser.add_argument(
        '--download-model',
        type=bool,
        default=False,
        help='Set to True, if the model weights and configurations \
                are not present on your local machine.')

    parser.add_argument('-t',
                        '--show-time',
                        type=bool,
                        default=False,
                        help='Show the time taken to infer each image.')

    FLAGS, unparsed = parser.parse_known_args()

    labels = open('darknet\\data\\obj.names').read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(
        'darknet/cfg/yolov3_custom.cfg',
        'darknet/backup/yolov3_custom_last.weights')

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]
    labels = open('darknet\\data\\obj.names').read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(
        'darknet/cfg/yolov3_custom.cfg',
        'darknet/backup/yolov3_custom_last.weights')

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    try:
        vid = cv.VideoCapture(vid_path)
        height, width = None, None
        writer = None
    except:
        raise 'Video cannot be loaded!\n\
                            Please check the path provided!'

    finally:
        while True:
            grabbed, frame = vid.read()
            # Checking if the complete video is read
            if not grabbed:
                break

            if width is None or height is None:
                height, width = frame.shape[:2]

            frame, _, _, _, _ = infer_image(net, obj_dec, layer_names, height,
                                            width, frame, colors, labels,
                                            FLAGS)

            if writer is None:
                # Initialize the video writer
                fourcc = cv.VideoWriter_fourcc(*"MJPG")
                writer = cv.VideoWriter(FLAGS.video_output_path, fourcc, 30,
                                        (frame.shape[1], frame.shape[0]), True)

            writer.write(frame)

        print("[INFO] Cleaning up...")
        writer.release()
        vid.release()
        cap = cv.VideoCapture('output.mp4')
        main()
        times = []
        boxes_num = []
        heights = []
        widths = []
        names = []
        for i in range(FLAGS.count):
            path = Gen_image_path(i)
            img = cv.imread(path)
            height, width = img.shape[:2]
            name = path[:(len(path) - 4)]
            name = name[4:]
            names.append(name)
            heights.append(height)
            widths.append(width)
            t1 = time.time()
            img, boxes, confidences, classid, idxs = infer_image(
                net, layer_names, height, width, img, colors, labels, FLAGS)
            t2 = time.time()
            times.append(t2 - t1)
            boxes_num.append(len(idxs))
            #print(idxs)
            cv.imwrite(Set_name_to_write(path), img)

        name_dict = {
            'Name': names,
            'Width': widths,
            'Height': heights,
            'Times': times,
            'Number of Detections': boxes_num
        }
        df = pd.DataFrame(name_dict)
        df.to_csv('Test_times_{}.csv'.format(j))
Exemple #20
0
def test_face_improved(path_in, path_out, suffix='face_improved'):
    parser = argparse.ArgumentParser()
    FLAGS, unparsed = parser.parse_known_args()

    FLAGS.model_path = '../yolov3-coco/'
    FLAGS.weights = '../yolov3-coco/yolov3-wider_16000.weights'
    FLAGS.config = '../yolov3-coco/yolov3-face.cfg'
    FLAGS.video_path = path_in
    FLAGS.video_output_path = f'{path_out}_{suffix}.avi'
    FLAGS.labels = '../yolov3-coco/coco-labels'
    FLAGS.confidence = 0.1
    FLAGS.threshold = 0.3
    FLAGS.download_model = False
    FLAGS.show_time = False

    emotion_model_path = '../models/emotion_model.hdf5'
    emotion_classifier = load_model(emotion_model_path)
    emotion_target_size = emotion_classifier.input_shape[1:3]
    emotion_labels = get_labels('fer2013')
    emotion_offsets = (20, 40)
    emotion_window = []
    frame_window = 10
    face_cascade = cv.CascadeClassifier(
        '../models/haarcascade_frontalface_default.xml')

    vid = cv.VideoCapture(FLAGS.video_path)
    height, width, writer = None, None, None

    labels = open(FLAGS.labels).read().strip().split('\n')
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    print(f'starting {suffix}')
    time_0 = time.time()

    frame_number = 0
    while True:
        grabbed, frame = vid.read()

        if not grabbed:
            break
        else:
            frame_number += 1

        if width is None or height is None:
            height, width = frame.shape[:2]

        img, boxes, confidences, classids, idxs = infer_image(
            net, layer_names, height, width, frame, colors, labels, FLAGS)

        gray_image = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        rgb_image = cv.cvtColor(img, cv.COLOR_BGR2RGB)

        faces = face_cascade.detectMultiScale(gray_image,
                                              scaleFactor=1.1,
                                              minNeighbors=5,
                                              minSize=(30, 30),
                                              flags=cv.CASCADE_SCALE_IMAGE)

        analysis_stastics.emotions.add_frame()

        for face_coordinates in faces:

            x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
            gray_face = gray_image[y1:y2, x1:x2]
            try:
                gray_face = cv.resize(gray_face, (emotion_target_size))
            except:
                continue

            gray_face = preprocess_input(gray_face, True)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_prediction = emotion_classifier.predict(gray_face)
            emotion_probability = np.max(emotion_prediction)
            emotion_label_arg = np.argmax(emotion_prediction)
            emotion_text = emotion_labels[emotion_label_arg]
            emotion_window.append(emotion_text)

            analysis_stastics.emotions.add_emotion(emotion_text)

            if len(emotion_window) > frame_window:
                emotion_window.pop(0)
            try:
                emotion_mode = mode(emotion_window)
            except:
                continue

            if emotion_text == 'angry':
                color = emotion_probability * np.asarray((255, 0, 0))
            elif emotion_text == 'sad':
                color = emotion_probability * np.asarray((0, 0, 255))
            elif emotion_text == 'happy':
                color = emotion_probability * np.asarray((255, 255, 0))
            elif emotion_text == 'surprise':
                color = emotion_probability * np.asarray((0, 255, 255))
            else:
                color = emotion_probability * np.asarray((0, 255, 0))

            color = color.astype(int)
            color = color.tolist()

            draw_text(face_coordinates, rgb_image, emotion_mode, color, 0, -45,
                      1, 1)

        img = cv.cvtColor(rgb_image, cv.COLOR_RGB2BGR)

        output_array = []
        for index in range(len(classids)):
            output_array.append({
                'name':
                labels[classids[index]],
                'percentage_probability':
                confidences[index] * 100
            })

        pfh.per_frame_handler(frame_number, output_array, suffix=suffix)

        if writer is None:
            fourcc = cv.VideoWriter_fourcc(*"MJPG")
            writer = cv.VideoWriter(FLAGS.video_output_path, fourcc, 30,
                                    (img.shape[1], img.shape[0]), True)

        writer.write(img)

    writer.release()
    vid.release()

    print(f'mode {suffix} finished, elapsed time : {time.time() - time_0}s')
Exemple #21
0
			raise 'Video cannot be loaded!\n\
                               Please check the path provided!'

		finally:
			while True:
				grabbed, frame = vid.read()

			    # Checking if the complete video is read
				if not grabbed:
					break

				if width is None or height is None:
					height, width = frame.shape[:2]
                #countcheck=0
                
				frame, _, _, _, _ = infer_image(net, layer_names, height, width, frame, colors, labels, FLAGS)

                
				if writer is None:
					# Initialize the video writer
					fourcc = cv.VideoWriter_fourcc(*"MJPG")
					writer = cv.VideoWriter(FLAGS.video_output_path, fourcc, 30, 
						            (frame.shape[1], frame.shape[0]), True)


				writer.write(frame)

			print ("[INFO] Cleaning up...")
			writer.release()
			vid.release()
Exemple #22
0
def yolo():
    parser = argparse.ArgumentParser()

    parser.add_argument('-m',
                        '--model-path',
                        type=str,
                        default='./yolov3-coco/',
                        help='The directory where the model weights and \
        configuration files are.')

    parser.add_argument('-w',
                        '--weights',
                        type=str,
                        default='./yolov3-coco/yolov3.weights',
                        help='Path to the file which contains the weights \
        for YOLOv3.')

    parser.add_argument(
        '-cfg',
        '--config',
        type=str,
        default='./yolov3-coco/yolov3.cfg',
        help='Path to the configuration file for the YOLOv3 model.')

    parser.add_argument('-vo',
                        '--video-output-path',
                        type=str,
                        default='./output.avi',
                        help='The path of the output video file')

    parser.add_argument('-l',
                        '--labels',
                        type=str,
                        default='./yolov3-coco/coco-labels',
                        help='Path to the file having the \
          labels in a new-line seperated way.')

    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
        probabiity less than the confidence value. \
        default: 0.5')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
        Non-Max Suppresion')

    parser.add_argument(
        '--download-model',
        type=bool,
        default=False,
        help='Set to True, if the model weights and configurations \
        are not present on your local machine.')

    parser.add_argument('-t',
                        '--show-time',
                        type=bool,
                        default=False,
                        help='Show the time taken to infer each image.')

    FLAGS, unparsed = parser.parse_known_args()
    #print(FLAGS)

    # Get the labels
    labels = open(FLAGS.labels).read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configutation to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    ################################

    height, width = frame.shape[:2]

    img, bboxes, _, classid, _ = infer_image(net, layer_names, height, width,
                                             frame, colors, labels, FLAGS)

    boxes = []  #It's a list now

    j = 0
    for i in classid:
        if i == 0:
            print("persons bounding box is: ", bboxes[j])
            boxes.append(bboxes[j].copy())
            #print(boxes[i])
        j = j + 1

    print(boxes)  #all faces

    ############################temp ###########33
    #for index,value in enumerate(boxes):
    global itr
    for i in range(len(boxes)):
        itr = itr + 1
        # Matching part
        labels = {}

        #Matching part end

        y = boxes[i][1]
        x = boxes[i][0]
        h = boxes[i][3]
        w = boxes[i][2]
        crop_img = img[y:y + h, x:x + w]
        #cv.imwrite(name,crop_img)

        detector = MTCNN()
        print("I am a detector phewww !")
        print(detector.detect_faces(crop_img))
        face_cropped = detector.detect_faces(crop_img)
        if (len(face_cropped) > 0):

            boxes_face = (face_cropped[0]['box'])
            y1 = boxes_face[1]
            x1 = boxes_face[0]
            h1 = boxes_face[3]
            w1 = boxes_face[2]
            crop_img_2 = crop_img[y1:y1 + h1, x1:x1 + w1]
            name = 'dataset/' + str("face") + str(itr) + '.jpg'
            #cv.imwrite(name,crop_img_2)

        #Matching Part
        path = 'dataset/Face' + str(itr)
        os.mkdir(path)
        name = 'dataset/Face' + str(itr) + '/' + str("person") + str(
            itr) + ".jpg"
        cv.imwrite(name, crop_img)
        name = 'dataset/Face' + str(itr) + '/' + str("face") + str(
            itr) + ".jpg"
        try:
            cv.imwrite(name, crop_img_2)
        except:
            pass
        #amtching

        #Faces_Train.py

        os.system('faces_train.py')

        #Faces_Train.py

        with open("labels.pickle", 'rb') as f:
            og_labels = pickle.load(f)
            labels = {v: k
                      for k, v in og_labels.items()
                      }  #talk to me about this ;) (key, value pair conversion)
        face_cascade = cv2.CascadeClassifier(
            'cascade/data/haarcascade_frontalface_alt2.xml'
        )  #only front of a face
        recognizer = cv2.face.LBPHFaceRecognizer_create()
        recognizer.read("trainer.yml")

        gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)

        faces = face_cascade.detectMultiScale(gray,
                                              scaleFactor=1.5,
                                              minNeighbors=5)
        #scaleFactor aghe peeche krke dekho
        print("Faces", faces)

        for (x, y, w, h) in faces:
            print(x, y, w, h)
            print("Amir")
            roi_gray = gray[y:y + h, x:x + w]  #roi -> Region of Interest
            roi_color = crop_img[y:y + h, x:x + w]
            #Recognition part
            id_, conf = recognizer.predict(
                roi_gray)  #id's and confidence level
            print(id_, conf, "moz")
            if (conf >= 25 and conf <= 85):
                print(id_)
                print("he")
                print(labels[id_])
                font = cv2.FONT_HERSHEY_SIMPLEX
                name = labels[id_]
                color = (255, 255, 255)
                stroke = 2
                cv2.putText(frame, name, (x, y), font, 1, color, stroke,
                            cv2.LINE_AA)

    ##########################temp done#########33

    my_tuple = []

    for i in bboxes:
        my_tuple.append(tuple(i))

    #print(my_tuple)

    # Create MultiTracker object
    multiTracker = cv2.MultiTracker_create()

    # Initialize MultiTracker
    colors_multi = []
    for bbox in my_tuple:
        multiTracker.add(createTrackerByName(trackerType), frame, bbox)
        colors_multi.append((randint(64, 255), randint(64,
                                                       255), randint(64, 255)))

    return multiTracker, colors_multi
Exemple #23
0
def yolo_detection(filename, fps_of_video):
    # YOLO Detection
    FLAGS = []

    parser = argparse.ArgumentParser()

    parser.add_argument('-m',
                        '--model-path',
                        type=str,
                        default='./yolov3-coco/',
                        help='The directory where the model weights and \
			  configuration files are.')

    parser.add_argument('-w',
                        '--weights',
                        type=str,
                        default='./yolov3-coco/yolov3.weights',
                        help='Path to the file which contains the weights \
			 	for YOLOv3.')

    parser.add_argument(
        '-cfg',
        '--config',
        type=str,
        default='./yolov3-coco/yolov3.cfg',
        help='Path to the configuration file for the YOLOv3 model.')

    parser.add_argument('-i',
                        '--image-path',
                        type=str,
                        help='The path to the image file')

    parser.add_argument('-v',
                        '--video-path',
                        type=str,
                        default='./templates/videos/' + filename,
                        help='The path to the video file')

    parser.add_argument('-vo',
                        '--video-output-path',
                        type=str,
                        default='./' + filename,
                        help='The path of the output video file')

    parser.add_argument('-l',
                        '--labels',
                        type=str,
                        default='./yolov3-coco/coco-labels',
                        help='Path to the file having the \
					labels in a new-line seperated way.')

    parser.add_argument('-c',
                        '--confidence',
                        type=float,
                        default=0.5,
                        help='The model will reject boundaries which has a \
				probabiity less than the confidence value. \
				default: 0.5')

    parser.add_argument('-th',
                        '--threshold',
                        type=float,
                        default=0.3,
                        help='The threshold to use when applying the \
				Non-Max Suppresion')

    parser.add_argument(
        '--download-model',
        type=bool,
        default=False,
        help='Set to True, if the model weights and configurations \
				are not present on your local machine.')

    parser.add_argument('-t',
                        '--show-time',
                        type=bool,
                        default=False,
                        help='Show the time taken to infer each image.')

    FLAGS, unparsed = parser.parse_known_args()

    # Download the YOLOv3 models if needed
    if FLAGS.download_model:
        subprocess.call(['./yolov3-coco/get_model.sh'])

# Get the labels
    labels = open(FLAGS.labels).read().strip().split('\n')

    # Intializing colors to represent each label uniquely
    colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')

    # Load the weights and configuration to form the pretrained YOLOv3 model
    net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)
    net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
    net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA)

    # Get the output layer names of the model
    layer_names = net.getLayerNames()
    layer_names = [
        layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()
    ]

    # If both image and video files are given then raise error
    if FLAGS.image_path is None and FLAGS.video_path is None:
        print('Neither path to an image or path to video provided')
        print('Starting Inference on Webcam')

# Do inference with given image
    if FLAGS.image_path:
        # Read the image
        try:
            img = cv.imread(FLAGS.image_path)
            height, width = img.shape[:2]
        except:
            raise 'Image cannot be loaded!\n\
                                Please check the path provided!'

        finally:
            img, _, _, _, _ = infer_image(net, layer_names, height, width, img,
                                          colors, labels, FLAGS)
            show_image(img)

    elif FLAGS.video_path:
        # Read the video
        try:
            vid = cv.VideoCapture(FLAGS.video_path)
            height, width = None, None
            writer = None
        except:
            raise 'Video cannot be loaded!\n\
                               Please check the path provided!'

        finally:
            try:
                connection = mysql.connector.connect(host='localhost',
                                                     user='******',
                                                     password='')
                if connection.is_connected():
                    cursor = connection.cursor()

                try:
                    print("Creating new database: 'vehicle_db'")
                    cursor.execute("create database vehicle_db")
                    print("Database created.")
                except:
                    print("Database already exists.")
                    print("Droping it")
                    cursor.execute("DROP DATABASE vehicle_db")
                    print("Creating New Database")
                    cursor.execute("create database vehicle_db")
                    print("New Database Created!")
                finally:
                    cursor.close()
                    connection.commit()
                    connection.close()

                    # Connecting to 'vehicle_db' database
                    connection = mysql.connector.connect(host='localhost',
                                                         database="vehicle_db",
                                                         user='******',
                                                         password='')
                    # Creating a new cursor for the new connection
                    cursor = connection.cursor()

                    print("Connected to database: 'vehicle_db'")

                    # removing all files from static folder
                    dir_path = os.path.dirname(os.path.realpath(__file__))
                    dir_path += "/static/"
                    files_to_remove = os.listdir(dir_path)
                    for file in files_to_remove:
                        os.remove(os.path.join(dir_path, file))

                while True:
                    grabbed, frame = vid.read()

                    # frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)

                    # Checking if the complete video is read
                    if not grabbed:
                        break

                    if width is None or height is None:
                        height, width = frame.shape[:2]

                    frame, _, _, _, _ = infer_image(net, layer_names, height,
                                                    width, frame, colors,
                                                    labels, connection, cursor,
                                                    FLAGS)

                    #cv.imshow('video_feed', frame)

                    if writer is None:
                        # Initialize the video writer
                        fourcc = cv.VideoWriter_fourcc(*"mp4v")
                        writer = cv.VideoWriter(
                            FLAGS.video_output_path, fourcc,
                            float(fps_of_video),
                            (frame.shape[1], frame.shape[0]), True)

                    writer.write(frame)

                print("[INFO] Cleaning up...")
                writer.release()
                vid.release()

            except Error as e:
                print("Connection error: {}".format(e))
            finally:
                if (connection.is_connected()):
                    cursor.close()
                    connection.commit()
                    connection.close()
                    print("Connection closed")

    else:
        # Infer real-time on webcam
        count = 0

        vid = cv.VideoCapture(0)
        while True:
            _, frame = vid.read()
            height, width = frame.shape[:2]

            if count == 0:
                frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
                                    height, width, frame, colors, labels, FLAGS)
                count += 1
            else:
                frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
                                    height, width, frame, colors, labels, FLAGS, boxes, confidences, classids, idxs, infer=False)
                count = (count + 1) % 6

            cv.imshow('webcam', frame)

            if cv.waitKey(1) & 0xFF == ord('q'):
                break
        vid.release()
        cv.destroyAllWindows()
Exemple #24
0
    if FLAGS.image_path is None and FLAGS.video_path is None:
        print('Neither path to an image or path to video provided')
        print('Starting Inference on Webcam')

    # Do inference with given image
    if FLAGS.image_path:
        # Read the image
        try:
            img = cv.imread(FLAGS.image_path)
            height, width = img.shape[:2]
        except:
            raise 'Image cannot be loaded!\n\
                               Please check the path provided!'

        finally:
            img, _, _, _, _ = infer_image(net, layer_names, height, width, img,
                                          colors, labels, FLAGS)
            show_image(img)

    elif FLAGS.video_path:
        # Read the video
        try:
            vid = cv.VideoCapture(FLAGS.video_path)
            height, width = None, None
            writer = None
        except:
            raise 'Video cannot be loaded!\n\
                               Please check the path provided!'

        finally:
            while True:
                grabbed, frame = vid.read()
Exemple #25
0
    def fun(frame, bb1):
        #_, frame = vid.read()
        #count = 0
        height, width = frame.shape[:2]

        rgb_image = frame[:, :, ::-1]

        #results = model.detect([rgb_image], verbose=0)
        #print("count",count)

        if len(bb1) == 0:


            frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
                                    height, width, frame, colors, labels, FLAGS)

            bb1 = get_car_boxes(boxes, classids)
            print("arra", bb1)
            #print(count)
            #count += 1
            return frame, bb1

        else:

            ans = []
            frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
                            height, width, frame, colors, labels, FLAGS)#, boxes, confidences, classids, idxs, infer=False)
            #count = (count + 1)
            #print("boxes",boxes)
            bb2 = []
            bb2 = get_car_boxes1(boxes, classids)
            print("arra2", bb2)
            ##print("arra",bb1)

            overlaps = compute_overlaps(bb1, bb2)
            ##print("iou",iou)
            ##print("overlaps",overlaps)

            for parking_area, overlap_areas in zip(bb1, overlaps):
                max_IoU_overlap = np.max(overlap_areas)
                #print("max",max_IoU_overlap)

                x, y, w, h = parking_area
                x = x + 30
                y = y + 30
                w = w + 30
                h = h + 30
                print("max", max_IoU_overlap)

                if (max_IoU_overlap) < 0:
                    for c in confidences:
                        if c > 0.6:
                            cv.rectangle(frame, (x, y), (x + w, y + h),
                                         (0, 255, 0), 3)
                else:
                    for c in confidences:
                        if c > 0.6:
                            cv.rectangle(frame, (x, y), (x + w, y + h),
                                         (0, 0, 255), 3)

                #ans.append(max_IoU_overlap)

            #print("ans",ans)
            print("fraem", frame)

            return frame, bb1