示例#1
1
def _main_(args):
    # Set up device
    device = mvnc.Device(args['device'])
    device.open()
    fifoIn, fifoOut = graph.allocate_with_fifos(device, graphfile)

    # Configuration stuff needed for postprocessing the predictions
    config_path = args['config']
    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    backend = config['model']['backend']
    input_size = (config['model']['input_size_h'],config['model']['input_size_w'])
    labels = config['model']['labels']
    max_box_per_image = config['model']['max_box_per_image']
    anchors = config['model']['anchors']
    gray_mode = config['model']['gray_mode']
    nb_class = 1


    #   Predict bounding boxes
    if video_mode:
        cap = FileVideoStream(source).start()
        time.sleep(1.0)
        fps = FPS().start()
        fps_img = 0.0
        counter = 0
        while True:
            start = time.time()
            image = cap.read()

            # Preprocessing
            if depth == 1:
                # convert video to gray
                image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                image = cv2.resize(image, input_size, interpolation = cv2.INTER_CUBIC)
                image = np.expand_dims(image, 2)
                fimage = np.array(image, dtype=np.float32)
            else:
                image = cv2.resize(image, input_size, interpolation = cv2.INTER_CUBIC)
                fimage = np.array(image, dtype=np.float32)

            #image = np.expand_dims(image, 0)
            fimage = np.divide(fimage, 255.)

            tm_inf = time.time()
            graph.queue_inference_with_fifo_elem(fifoIn, fifoOut, fimage, 'user object')
            prediction, _ = fifoOut.read_elem()

            #prediction = np.add(fimage, 128)
            grid = 8
            prediction = np.reshape(prediction, (grid, grid, 5, 4 + 1 + nb_class))
            # prediction = np.multiply(prediction, 255)

            fps_img  = ( fps_img + ( 1 / (time.time() - start) ) ) / 2
            print( "Inference time: {:.4f}".format(time.time() - tm_inf) )

            # predictions decoding
            boxes  = decode_netout(prediction, anchors, nb_class)
            image = draw_boxes(image, boxes, config['model']['labels'])

            image = cv2.putText(image, "fps: {:.2f}".format(fps_img), (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 4 )
            cv2.imshow("Press q to quit", image)
            fps.update()

            #if counter == 10:
                #print(image.sum(), boxes)
            #    time.sleep(1)
            counter += 1

            if cv2.getWindowProperty( "Press q to quit", cv2.WND_PROP_ASPECT_RATIO ) < 0.0:
                print("Window closed" )
                break
            elif cv2.waitKey( 1 ) & 0xFF == ord( 'q' ):
                print( "Q pressed" )
                break
        fps.stop()
        print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
        print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
        cap.release()

    else:
        #detected_images_path = os.path.join(image_path, "detected")
        #if not os.path.exists(detected_images_path):
        #    os.mkdir(detected_images_path)
        images = list(list_images(source))
        for fname in images[100:]:
            image = cv2.imread(fname)
            tm_inf = time.time()
            boxes = yolo.predict(image)
            print( "Inference time: {:.4f}".format(time.time() - tm_inf) )
            image = draw_boxes(image, boxes, config['model']['labels'])
            cv2.imshow("Press q to quit", image)

            if cv2.waitKey( 1 ) & 0xFF == ord( 'q' ):
                break

            time.sleep(2)
        #    fname = os.path.basename(fname)
        #    cv2.imwrite(os.path.join(image_path, "detected", fname), image)
    cv2.destroyAllWindows()
class VideoCamera(object):
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        # self.video = cv2.VideoCapture(0)
        # If you decide to use video.mp4, you must have this file in the folder
        # as the main.py.
        self.video = FileVideoStream("cropvideo.mp4").start()

    def __del__(self):
        self.video.release()

    def get_frame(self):

        image = self.video.read()
        faceCascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(gray, 1.1, 4)

        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # video stream.

        for (x, y, w, h) in faces:
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()
示例#3
0
class CustomStream:
    def __init__(self, src=0, use_cv2=False):
        if use_cv2:
            self.obj = cv2.VideoCapture(src)
        elif src == 0:
            self.obj = WebcamVideoStream(src)
        elif src != 0:
            self.obj = FileVideoStream(src)

    def isOpened(self):
        if isinstance(self.obj, cv2.VideoCapture):
            return self.obj.isOpened()
        return self.obj.stream.isOpened()

    def start(self):
        self.obj.start()
        return self

    def update(self):
        self.obj.update()

    def read(self):
        if isinstance(self.obj, cv2.VideoCapture):
            return self.obj.read()
        return not self.obj.stopped, self.obj.read()

    def stop(self):
        self.obj.stop()
        if isinstance(self.obj, cv2.VideoCapture):
            self.obj.release()
        else:
            self.obj.stream.release()

    def set(self, propId, value):
        if isinstance(self.obj, cv2.VideoCapture):
            self.obj.set(propId, value)
            return
        self.obj.stream.set(propId, value)

    def get(self, propId):
        if isinstance(self.obj, cv2.VideoCapture):
            self.obj.get(propId)
            return
        return self.obj.stream.get(propId)
示例#4
0
def recognise_face(data,input,args):
# initialize the pointer to the video file and the video writer
    print("[INFO] processing video...")
    stream = cv2.VideoCapture(args["input"])
    stream = FileVideoStream(args["input"]).start()
    # fps =FPS(args["input"])#.start()
    # stream =fps
    writer = None

    # loop over frames from the video file stream
    while stream.more():
        # grab the next frame
        frame = stream.read()

        # if the frame was not grabbed, then we have reached the
        # end of the stream
        # if not grabbed:
        #     break

        # convert the input frame from BGR to RGB then resize it to have
        # a width of 750px (to speedup processing)
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        rgb = imutils.resize(frame, width=750)
        r = frame.shape[1] / float(rgb.shape[1])

        # detect the (x, y)-coordinates of the bounding boxes
        # corresponding to each face in the input frame, then compute
        # the facial embeddings for each face

        boxes = face_recognition.face_locations(frame,
            model=args["detection_method"])
        encodings = face_recognition.face_encodings(rgb, boxes)
        names = []
        results =[]

        # loop over the facial embeddings
        for encoding in encodings:
            # attempt to match each face in the input image to our known
            # encodings
            matches = face_recognition.compare_faces(data["encodings"],
                encoding)
            name = "Unknown_0"

            # check to see if we have found a match
            if True in matches:
                # find the indexes of all matched faces then initialize a
                # dictionary to count the total number of times each face
                # was matched
                matchedIdxs = [i for (i, b) in enumerate(matches) if b]
                counts = {}

                # loop over the matched indexes and maintain a count for
                # each recognized face face
                for i in matchedIdxs:
                    name = data["names"][i]
                    key = str(name) + "_" +str(data["id"][i])
                    counts[key] = counts.get(key, 0) + 1

                # determine the recognized face with the largest number
                # of votes (note: in the event of an unlikely tie Python
                # will select first entry in the dictionary)
                name = max(counts, key=counts.get)

            # update the list of names, id
            name = name.split("_")
            id = int(name[-1])
            name = name[:-1]
            name = " ".join(name)
            names.append(name)
            results.append(dict(id=id, name=name))

        # loop over the recognized faces
        for ((top, right, bottom, left), name,res) in zip(boxes, names,results):
            # rescale the face coordinates
            top = int(top * r)
            right = int(right * r)
            bottom = int(bottom * r)
            left = int(left * r)

            # draw the predicted face name on the image
            cv2.rectangle(frame, (left, top), (right, bottom),
                (0, 255, 0), 2)
            y = top - 15 if top - 15 > 15 else top + 15
            cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 255, 0), 2)
            yield res
        # if the video writer is None *AND* we are supposed to write
        # the output video to disk initialize the writer
        if writer is None and args["output"] is not None:
            fourcc = cv2.VideoWriter_fourcc(*"MJPG")
            writer = cv2.VideoWriter(args["output"], fourcc, 24,
                (frame.shape[1], frame.shape[0]), True)

        # if the writer is not None, write the frame with recognized
        # faces t odisk
        if writer is not None:
            writer.write(frame)

        # check to see if we are supposed to display the output frame to
        # the screen
        if args["display"] > 0:
            cv2.imshow("Frame", frame)
            key = cv2.waitKey(1) & 0xFF

            # if the `q` key was pressed, break from the loop
            if key == ord("q"):
                break

    # close the video file pointers
    stream.release()

    # check to see if the video writer point needs to be released
    if writer is not None:
        writer.release()
        # draw the total number of social distancing violations on the
        # output frame
        text = "Social Distancing Violations: {}".format(len(violate))
        cv2.putText(frame, text, (10, frame.shape[0] - 25),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.85, (0, 0, 255), 3)

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

# show the output frame
    cv2.imshow("Frame", frame)
    fps.update()
    #for i in range(50000000):
    #    pass
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

    # update the FPS counte

# 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
cv2.destroyAllWindows()
vs.release()
    (h, w) = frame.shape[:2]

    # blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size, mean, swapRB=True)
    blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
                                 (300, 300), (104.7, 177.0, 123.0), True)

    net.setInput(blob)
    detections = net.forward()

    for i in range(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence < args["confidence"]:
            continue

        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")

        text = "{:.2f}%".format(confidence * 100)
        y = startY - 10 if startY - 10 > 10 else startY + 10
        cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 255), 2)
        cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45,
                    (0, 0, 255), 2)

    cv2.imshow("Frame", frame)
    cv2.waitKey(1)

    vs.update()

vs.stop()
fvs.release()
cv2.destroyAllWindows()
示例#7
0
class IP(object):
    def __init__(self):
        #self.cap = cv2.VideoCapture(1)

        self.fvs = FileVideoStream(1)

        # self.fvs.stream.set(3,FULL_WIDTH)
        # self.fvs.stream.set(4,FULL_HEIGHT)

        self.fvs.start()

        #self.fvs = FileVideoStream(0).start

        # self.cap.set(3,FULL_WIDTH) #3 - WIDTH
        # self.cap.set(4,FULL_HEIGHT)  #4 - HEIGHT

    def get_image(self):
        #ret,image = self.cap.read()
        # print image
        image = self.fvs.read()
        i = 0
        #image = np.dstack([image, image, image])
        #print ("SHAPE : ",image.shape)
        image = imutils.resize(image, width=FULL_WIDTH)  #,height=FULL_HEIGHT)
        print("SHAPE : ", image.shape)
        # while not np.size(image,0) == FULL_HEIGHT or not np.size(image,1) == FULL_WIDTH :
        #      print ("trying",i)
        #      print ("SHAPE : ",image.shape)
        #      i += 1
        #      image = self.fvs.read()
        #      #ret,image = self.cap.read()
        return image

    def perspective_transform(self, image, pts1, pts2):
        return cv2.warpPerspective(image,
                                   cv2.getPerspectiveTransform(pts1, pts2),
                                   (FINAL_WIDTH, FINAL_HEIGHT))

    def rgb2gray(self, image):
        return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    def rgb2hsv(self, image):
        return cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    def threshold_image(self, image, threshold, max_val):
        return cv2.threshold(image, threshold, max_val, cv2.THRESH_BINARY)

    def display_image(self, image, window_name="Image"):
        cv2.imshow(window_name, image)
        if cv2.waitKey(1) == ord('q'):
            cv2.destroyAllWindows()
            self.fvs.stop()
            return 0
        return 1

    def find_contours(self, image):
        major, _, _ = cv2.__version__.split('.')
        if major == '3':
            _, contour, hierarchy = cv2.findContours(image, cv2.RETR_TREE, 2)
        else:
            contour, hierarchy = cv2.findContours(image, cv2.RETR_TREE, 2)
        return contour, hierarchy

    def find_area(self, contour):
        return cv2.contourArea(contour)

    def find_moments(self, contour):
        return cv2.moments(contour)

    def crop_image(self, image, center, amount):
        #print int(center[1])-amount,int(center[0])-amount
        #print int(center[1])+amount,int(center[0])+amount
        if int(center[1]) - amount >= 0 and int(center[0]) - amount >= 0:
            if int(center[1]) + amount <= FINAL_HEIGHT and int(
                    center[0]) + amount <= FINAL_WIDTH:
                img = image[(int(center[1]) - amount):(int(center[1]) +
                                                       amount),
                            (int(center[0]) - amount):(int(center[0]) +
                                                       amount)]
                return img
            else:
                return -1
        else:
            return -1

    def get_hsv_mask(self, image, lower, upper):
        return cv2.inRange(image, lower, upper)

    def end_all(self):
        self.fvs.release()
        cv2.destroyAllWindows()

    def draw_grid(self, im2, x_grid_num, y_grid_num):
        #ret,frame = self.cap.read()
        row = [0 for i in range(18)]
        mat = np.zeros((12, 18), dtype=np.uint64)
        #for i in range(12):
        #	mat.append([])
        X = im2.shape[1]
        Y = im2.shape[0]
        start_x = 0
        start_y = 0
        end_x = X / 18
        end_y = Y / 12

        for i in range(18):
            for j in range(12):
                if i not in range(x_grid_num - 1,
                                  x_grid_num + 2) or j not in range(
                                      y_grid_num - 1, y_grid_num + 2):
                    im2 = cv2.rectangle(
                        im2, (start_x + (X / 18) * i, start_y + (Y / 12) * j),
                        (end_x + (X / 18) * i, end_y + (Y / 12) * j),
                        (0, 0, 255))
                    mat[j][i] = 0
                else:
                    im2 = cv2.rectangle(
                        im2, (start_x + (X / 18) * i, start_y + (Y / 12) * j),
                        (end_x + (X / 18) * i, end_y + (Y / 12) * j),
                        (0, 0, 255), -1)
                    mat[j][i] = 1
                #print "HI", mat[j][i]
        '''
    	print "Printing mat..."
    	for i in range(12):
    		s = ""
    		for j in range(18):
    			s += str(mat[i][j]) + " "
    		print s
    	'''
        MAT = np.copy(mat)
        #print "MAT.shape", MAT.shape
        return im2, MAT

    '''
                    
                    ##increase image size and resoulation
                    new_img = img[int(bbox[1]):int(bbox[3]), int(bbox[0]):int(bbox[2])]
                    new_img = cv2.resize(new_img, (360, 360), interpolation = cv2.INTER_NEAREST)

                    cv2.imwrite(directory1 + f"image{b1}.jpg", new_img)
                                
                    if count > 1:
                        break
                #current_count += 1


    total_count = len(set(counter))
    #cv2.putText(img, "Current Vehicle Count: " + str(current_count), (0, 80), 0, 1, (0, 0, 255), 2)
    cv2.putText(img, "Total Vehicle Count: " + str(total_count), (0,130), 0, 1, (0,0,255), 2)


    fps = 1./(time.time()-t1)
    cv2.putText(img, "FPS: {:.2f}".format(fps), (0,30), 0, 1, (0,0,255), 2)
    #cv2.resizeWindow('output', 1024, 768)
    cv2.imshow('output', img)
    out.write(img)


    if cv2.waitKey(1) == ord('q'):
        break
    
    
vid.release()
out.release()
cv2.destroyAllWindows()
示例#9
0
def _main_():
    config_path = 'ncsmodel/config.json'
    image_path = 'videos/uba.mp4'
    keras.backend.tensorflow_backend.set_session(get_session())

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    input_size = (config['model']['input_size_h'],
                  config['model']['input_size_w'])
    labels = config['model']['labels']
    max_box_per_image = config['model']['max_box_per_image']
    anchors = config['model']['anchors']
    gray_mode = config['model']['gray_mode']
    nb_class = 1

    # Net Config
    NN = Net(load_weights=True)
    cv2_image = cv2.imread("images/person.jpg", 0)
    image = NN.image

    if config['model']['gray_mode']:
        depth = 1
    else:
        depth = 3

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        if image_path[-4:] == '.mp4':
            video_out = image_path[:-4] + '_detected' + image_path[-4:]
            cap = FileVideoStream(image_path).start()
            time.sleep(1.0)
            fps = FPS().start()
            fps_img = 0.0
            counter = 0
            tf_image = NN.image

            while True:
                start = time.time()
                image = cap.read()
                print(image.min(), image.max(), image.mean(), image.shape)

                if depth == 1:
                    # convert video to gray
                    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                    # resize for plotting
                    resized_image = cv2.resize(gray_image,
                                               input_size,
                                               interpolation=cv2.INTER_CUBIC)
                    # convert to float
                    image_data = np.array(resized_image, dtype=np.float16)
                    image_data /= 255.
                    # dimensions for inference (1, w, h, c)
                    image_data = np.expand_dims(np.expand_dims(image_data, 0),
                                                3)  # 1, 256, 256, 1
                    # dimension for plot (w, h, c)
                    plot_image = np.expand_dims(resized_image, 2)
                else:
                    if counter == 1:
                        print("Color image")
                    image_data = cv2.resize(image,
                                            input_size,
                                            interpolation=cv2.INTER_CUBIC)
                    #image = np.array(image, dtype='f')
                #image = np.divide(image, 255.)

                tm_inf = time.time()
                netout = sess.run(NN.predict(),
                                  feed_dict={tf_image: image_data})
                netout = np.reshape(np.squeeze(netout, axis=0), (8, 8, 5, 6))
                boxes = decode_netout(netout, anchors, nb_class)

                fps_img = (fps_img + (1 / (time.time() - start))) / 2
                #print( "Inference time: {:.4f}".format(time.time() - tm_inf) )
                print(plot_image.shape)
                image = draw_boxes(plot_image, boxes, labels)
                #image = cv2.putText(image, "fps: {:.2f}".format(fps_img), (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 4 )
                cv2.imshow("Press q to quit", image)
                fps.update()

                #if counter == 10:
                #print(image.sum(), boxes)
                #    time.sleep(1)
                counter += 1

                if cv2.getWindowProperty("Press q to quit",
                                         cv2.WND_PROP_ASPECT_RATIO) < 0.0:
                    print("Window closed")
                    break
                elif cv2.waitKey(1) & 0xFF == ord('q'):
                    print("Q pressed")
                    break
            fps.stop()
            print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
            print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
            cap.release()

        else:
            images = list(list_images(image_path))
            for fname in images[100:]:
                image = cv2.imread(fname)
                tm_inf = time.time()
                boxes = yolo.predict(image)
                print("Inference time: {:.4f}".format(time.time() - tm_inf))
                image = draw_boxes(image, boxes, config['model']['labels'])
                cv2.imshow("Press q to quit", image)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
                time.sleep(2)
        cv2.destroyAllWindows()
示例#10
0
class VideoCamera(object):
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        # self.video = cv2.VideoCapture(0)
        # If you decide to use video.mp4, you must have this file in the folder
        # as the main.py.
        self.video = FileVideoStream("cropvideo.mp4").start()
    
    def __del__(self):
        self.video.release()

    def detect_people(self, frame):

        (rects, weights) = hog.detectMultiScale(frame, winStride=(8, 8), padding=(16, 16), scale=1.06)
        rects = non_max_suppression(rects, probs=None, overlapThresh=0.65)
        for (x, y, w, h) in rects:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
        return frame


    def detect_face(self, frame):

        faces = face_cascade.detectMultiScale(frame, 1.1, 2, 0, (20, 20))
        return faces


    def draw_faces(self, frame, faces):

        for (x, y, w, h) in faces:
            xA = x
            yA = y
            xB = x + w
            yB = y + h
            cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)
        return frame


    def background_subtraction(self, previous_frame, frame_resized_grayscale, min_area):

        frameDelta = cv2.absdiff(previous_frame, frame_resized_grayscale)
        thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
        thresh = cv2.dilate(thresh, None, iterations=2)
        cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        temp = 0
        for c in cnts:
            if cv2.contourArea(c) > min_area:
                temp = 1
        return temp


    
    def get_frame(self):

        frame = self.video.read()

        frame_resized_grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
        frame_processed = self.detect_people(frame)

        faces = self.detect_face(frame)

        frame_processed = self.draw_faces(frame_processed, faces)

        ret, jpeg = cv2.imencode('.jpg', frame_processed)

        return jpeg.tobytes()
                k = 0

                if class_names[best_class_indices[k]] == "id1":
                    cv2.imwrite(
                        "/home/sai/kato/prediction/dataset/id1/User." +
                        str(id) + "." + str(sampleNum) + ".jpg", aligned)

                elif class_names[best_class_indices[k]] == "id2":
                    cv2.imwrite(
                        "/home/sai/kato/prediction/dataset/id2/User." +
                        str(id) + "." + str(sampleNum) + ".jpg", aligned)

                elif class_names[best_class_indices[k]] == "id3":
                    cv2.imwrite(
                        "/home/sai/kato/prediction/dataset/id3/User." +
                        str(id) + "." + str(sampleNum) + ".jpg", aligned)

                elif class_names[best_class_indices[k]] == "id4":
                    cv2.imwrite(
                        "/home/sai/kato/prediction/dataset/id4/User." +
                        str(id) + "." + str(sampleNum) + ".jpg", aligned)

                else:
                    print("unknown")

            k = cv2.waitKey(1)
            if k == ord('x'):
                break

        camera.release()
        cv2.destroyAllWindows()