Exemple #1
0
def cv2_demo(net, transform):
    def predict(frame):
        height, width = frame.shape[:2]
        x = torch.from_numpy(transform(frame)[0]).permute(2, 0, 1)
        x = Variable(x.unsqueeze(0))
        y = net(x)  # forward pass
        detections = y.data
        # scale each detection back up to the image
        scale = torch.Tensor([width, height, width, height])
        for i in range(detections.size(1)):
            j = 0
            while detections[0, i, j, 0] >= 0.6:
                pt = (detections[0, i, j, 1:] * scale).cpu().numpy()
                cv2.rectangle(frame,
                              (int(pt[0]), int(pt[1])),
                              (int(pt[2]), int(pt[3])),
                              COLORS[i % 3], 2)
                cv2.putText(frame, labelmap[i - 1], (int(pt[0]), int(pt[1])),
                            FONT, 2, (255, 255, 255), 2, cv2.LINE_AA)
                j += 1
        return frame

    # start video stream thread, allow buffer to fill
    print("[INFO] starting threaded video stream...")
    stream = WebcamVideoStream(src=0).start()  # default camera
    time.sleep(1.0)
    # start fps timer
    # loop over frames from the video file stream
    while True:
        # grab next frame
        frame = stream.read()
        key = cv2.waitKey(1) & 0xFF

        # update FPS counter
        fps.update()
        frame = predict(frame)

        # keybindings for display
        if key == ord('p'):  # pause
            while True:
                key2 = cv2.waitKey(1) or 0xff
                cv2.imshow('frame', frame)
                if key2 == ord('p'):  # resume
                    break
        cv2.imshow('frame', frame)
        if key == 27:  # exit
            break
Exemple #2
0
    def __init__(self, center=int(cvsettings.CAMERA_WIDTH / 2), debug=False, is_usb_webcam=True, period_s=0.025):
        # Our video stream
        # If its not a usb webcam then get pi camera
        if not is_usb_webcam:
            self.vs = PiVideoStream(resolution=(cvsettings.CAMERA_WIDTH, cvsettings.CAMERA_HEIGHT))
            # Camera cvsettings
            self.vs.camera.shutter_speed = cvsettings.SHUTTER
            self.vs.camera.exposure_mode = cvsettings.EXPOSURE_MODE
            self.vs.camera.exposure_compensation = cvsettings.EXPOSURE_COMPENSATION
            self.vs.camera.awb_gains = cvsettings.AWB_GAINS
            self.vs.camera.awb_mode = cvsettings.AWB_MODE
            self.vs.camera.saturation = cvsettings.SATURATION
            self.vs.camera.rotation = cvsettings.ROTATION
            self.vs.camera.video_stabilization = cvsettings.VIDEO_STABALIZATION
            self.vs.camera.iso = cvsettings.ISO
            self.vs.camera.brightness = cvsettings.BRIGHTNESS
            self.vs.camera.contrast = cvsettings.CONTRAST

        # Else get the usb camera
        else:
            self.vs = WebcamVideoStream(src=0)
            self.vs.stream.set(cv2.CAP_PROP_FRAME_WIDTH, cvsettings.CAMERA_WIDTH)
            self.vs.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, cvsettings.CAMERA_HEIGHT)

        # Has camera started
        self.camera_started = False
        self.start_camera()  # Starts our camera

        # To calculate our error in positioning
        self.center = center

        # To determine if we actually detected lane or not
        self.detected_lane = False

        # debug mode on? (to display processed images)
        self.debug = debug

        # Time interval between in update (in ms)
        # FPS = 1/period_s
        self.period_s = period_s

        # Starting time
        self.start_time = time.time()
    def __init__(self, cam_L_src, cam_R_src, display_frames=True, threshold=110):

        # initialize camera feed threads
        self.capL = WebcamVideoStream(src=cam_L_src).start()
        time.sleep(0.5)
        self.capR = WebcamVideoStream(src=cam_R_src).start()

        self.stop = False

        self.display_frames = display_frames

        # initialize windows
        if self.display_frames == True:
            cv2.namedWindow('Depth Map')
            cv2.namedWindow('Threshold')
            cv2.namedWindow('Shapes')

        # import calibration matrices
        self.undistMapL = np.load('calibration/undistortion_map_left.npy')
        self.undistMapR = np.load('calibration/undistortion_map_right.npy')
        self.rectMapL = np.load('calibration/rectification_map_left.npy')
        self.rectMapR = np.load('calibration/rectification_map_right.npy')

        # initialize blank frames
        self.rectL = np.zeros((640, 480, 3), np.uint8)
        self.rectR = np.zeros((640, 480, 3), np.uint8)

        self.quadrant_near_object = np.zeros((3, 3), dtype=bool)
        self.threshold = threshold

        self.exec_time_sum = 0
        self.frame_counter = 0
        self.fps = 0

        self.no_object_left_column = False
        self.no_object_mid_column = False
        self.no_object_right_column = False
        self.no_object_bottom_row = False
        self.no_object_mid_row = False
        self.no_object_top_row = False
Exemple #4
0
# Ref: http://dlib.net/face_landmark_detection.py.html
# Ref: https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/

# loading the DLIB's face detector/predictor
dlib_detector = dlib.get_frontal_face_detector()
print("Logs: DLIB Face Landmark Predictor has been loaded...")

# creating a variable for face landmark prediction, and load the pre-trained model on facial landmarks
dlib_predictor = dlib.shape_predictor(shape_predictor_file)

#Fetching the landmarks for the eyes and storing the beginning and end of the left and right eyes
(left_beg, left_end) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(right_beg, right_end) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

# initializing the camera for live detection
cam_stream = WebcamVideoStream(src=0).start()
print("Logs: Camera started ~ 📷 ~ ")

# Starting while loop for the frames of the camera streams
while True:

    #Read the camera stream frame by frame
    stream_frame = cam_stream.read()
    #print("Logs: Reading the stream frame by frame...")

    #Resizing the frame width to 600
    stream_frame = imutils.resize(stream_frame, width=600)

    # convert the frames into grayscale
    gray_scale = cv2.cvtColor(stream_frame, cv2.COLOR_BGR2GRAY)
Exemple #5
0
# list of tracked points
yellowLower = (5, 25, 130)
yellowUpper = (60, 220, 320)

highestY = 0
farLeft = 600
farRight = 0

# defines udp server address for jetson
HOST = "0.0.0.0"
PORT = 5806

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((HOST, PORT))

camera = WebcamVideoStream(src=0).start()

message = None

# keep looping
while True:
        try:
                cycle, rioAddress = sock.recvfrom(65507)

        except Exception:
                cycle = None
                rioAddress = None

                
	# grab the current frame
	(grabbed, frame) = camera.read()
Exemple #6
0

def recvallVideo(size):
    databytes = b''
    while len(databytes) != size:
        to_read = size - len(databytes)
        if to_read > (5000 * CHUNK):
            databytes += serverVideoSocket.recv(5000 * CHUNK)
        else:
            databytes += serverVideoSocket.recv(to_read)
    return databytes


serverVideoSocket = socket(family=AF_INET, type=SOCK_STREAM)
serverVideoSocket.bind((HOST, PORT_VIDEO))
wvs = WebcamVideoStream(0).start()

serverAudioSocket = socket(family=AF_INET, type=SOCK_STREAM)
serverAudioSocket.bind((HOST, PORT_AUDIO))

print('Server listening ...')
serverVideoSocket.listen(4)
serverAudioSocket.listen(4)
serverVideoSocket.accept()
print('Video connected ...')
serverAudioSocket.accept()
print('Audio connected ...')

audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT,
                    channels=CHANNELS,
Exemple #7
0
# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

# do a bit of cleanup
stream.release()
cv2.destroyAllWindows()

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

# created a *threaded* video stream, allow the camera sensor to warmup,
# and start the FPS counter
print("[INFO] sampling THREADED frames from webcam...")
vs = WebcamVideoStream(src=1).start()
fps = FPS().start()

# loop over some frames...this time using the threaded stream
while fps._numFrames < args["num_frames"]:
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    frame = vs.read()
    frame = imutils.resize(frame, width=400)

    # check to see if the frame should be displayed to our screen
    if args["display"] > 0:
        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF

    # update the FPS counter
import cv2
from imutils.video import WebcamVideoStream
import imutils

vs = WebcamVideoStream(src=0).start()
avg = None

while True:

    frame = vs.read()
    frame = imutils.resize(frame, width=400)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    if avg is None:
        avg = gray.copy().astype("float")
        continue

    cv2.accumulateWeighted(gray, avg, 0.1)
    frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg))
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
    thresh = cv2.dilate(thresh, None, iterations=2)
    _, contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in contours:
        if cv2.contourArea(c) < 4000:
            print("Tiny")
            continue

        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
def main():
    if rsFlag:
        camera = d435.d435()
        pipeline, align, clipping_distance, NIR = camera.setup()
    else:
        camera = WebcamVideoStream(camera_index).start()

    # load the COCO class labels our YOLO model was trained on
    labelsPath = PARAM_PATH + classes[classes_index]
    LABELS = open(labelsPath).read().strip().split("\n")

    # initialize a list of colors to represent each possible class label
    np.random.seed(42)
    COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8")

    # derive the paths to the YOLO weights and model configuration
    if not os.path.isfile(PARAM_PATH + weights[classes_index]):
        os.system(PARAM_PATH + "getWeights.sh")
    weightsPath = PARAM_PATH + weights[classes_index]
    configPath = PARAM_PATH + config[classes_index]

    # load our YOLO object detector trained on COCO dataset (80 classes)
    #print("[INFO] loading YOLO from disk...")
    net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
    print("Classify objects is now running...")
    try:
        while True:
            if rsFlag:
                # Wait for a coherent pair of frames: depth and color
                frames = pipeline.wait_for_frames()

                # Align the depth frame to color frame
                aligned_frames = align.process(frames)

                # Get aligned frames
                aligned_depth_frame = aligned_frames.get_depth_frame(
                )  # aligned_depth_frame is a 640x480 depth image
                color_aligned_frame = aligned_frames.get_color_frame()

                if NIR:
                    nir_lf_frame = frames.get_infrared_frame(1)
                    nir_rg_frame = frames.get_infrared_frame(2)

                if NIR:
                    if not aligned_depth_frame or not color_aligned_frame or not nir_lf_frame or not nir_rg_frame:
                        continue
                else:
                    if not aligned_depth_frame or not color_aligned_frame:
                        continue

                # Convert images to numpy arrays
                depth_image = np.asanyarray(aligned_depth_frame.get_data())
                image = np.asanyarray(color_aligned_frame.get_data())

                # Remove background - Set pixels further than clipping_distance to grey
                grey_color = 153
                depth_image_3d = np.dstack(
                    (depth_image, depth_image, depth_image
                     ))  # depth image is 1 channel, color is 3 channels
                bg_removed = np.where((depth_image_3d > clipping_distance) |
                                      (depth_image_3d <= 0), grey_color, image)

                if NIR:
                    nir_lf_image = np.asanyarray(nir_lf_frame.get_data())
                    nir_rg_image = np.asanyarray(nir_rg_frame.get_data())
                # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
                depth_colormap = cv2.applyColorMap(
                    cv2.convertScaleAbs(depth_image, alpha=0.03),
                    cv2.COLORMAP_JET)
            else:
                # Capture frame-by-frame
                image = camera.read()
            (H, W) = image.shape[:2]
            if invColor:
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            # determine only the *output* layer names that we need from YOLO
            ln = net.getLayerNames()
            ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]

            # construct a blob from the input image and then perform a forward
            # pass of the YOLO object detector, giving us our bounding boxes and
            # associated probabilities
            blob = cv2.dnn.blobFromImage(image,
                                         1 / 255.0, (416, 416),
                                         swapRB=True,
                                         crop=False)
            net.setInput(blob)
            start = time.time()
            layerOutputs = net.forward(ln)
            end = time.time()

            # show timing information on YOLO
            # print("[INFO] YOLO took {:.6f} seconds".format(end - start))

            # initialize our lists of detected bounding boxes, confidences, and
            # class IDs, respectively
            boxes = []
            confidences = []
            classIDs = []

            # loop over each of the layer outputs
            for output in layerOutputs:
                # loop over each of the detections
                for detection in output:
                    # extract the class ID and confidence (i.e., probability) of
                    # the current object detection
                    scores = detection[5:]
                    classID = np.argmax(scores)
                    confidence = scores[classID]

                    # filter out weak predictions by ensuring the detected
                    # probability is greater than the minimum probability
                    if confidence > conf:
                        # scale the bounding box coordinates back relative to the
                        # size of the image, keeping in mind that YOLO actually
                        # returns the center (x, y)-coordinates of the bounding
                        # box followed by the boxes' width and height
                        box = detection[0:4] * np.array([W, H, W, H])
                        (centerX, centerY, width, height) = box.astype("int")

                        # use the center (x, y)-coordinates to derive the top and
                        # and left corner of the bounding box
                        x = int(centerX - (width / 2))
                        y = int(centerY - (height / 2))

                        # update our list of bounding box coordinates, confidences,
                        # and class IDs
                        boxes.append([x, y, int(width), int(height)])
                        confidences.append(float(confidence))
                        classIDs.append(classID)

            # apply non-maxima suppression to suppress weak, overlapping bounding
            # boxes
            idxs = cv2.dnn.NMSBoxes(boxes, confidences, conf, threshold)

            # ensure at least one detection exists
            if len(idxs) > 0:
                # loop over the indexes we are keeping
                for i in idxs.flatten():
                    # extract the bounding box coordinates
                    (x, y) = (boxes[i][0], boxes[i][1])
                    (w, h) = (boxes[i][2], boxes[i][3])

                    # draw a bounding box rectangle and label on the image
                    color = [int(c) for c in COLORS[classIDs[i]]]
                    cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
                    text = "{}: {:.4f}".format(LABELS[classIDs[i]],
                                               confidences[i])
                    cv2.putText(image, text, (x, y - 5),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
                    if rsFlag:
                        cv2.rectangle(depth_colormap, (x, y), (x + w, y + h),
                                      color, 2)
                        cv2.putText(depth_colormap, text, (x, y - 5),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
            # show the output image
            if rsFlag and stackFlag:
                images1 = np.hstack((image, depth_colormap))
                cv2.imshow("Intel RealSense D435 RGB and Depth Map ", images1)
                if NIR:
                    images2 = np.hstack((nir_lf_image, nir_rg_image))
                    cv2.imshow("Intel RealSense D435 NIRs Left and Right ",
                               images2)

            elif rsFlag and not stackFlag:
                cv2.imshow("Intel RealSense D435 RGB ", image)
                cv2.imshow("Intel RealSense D435 depth map ", depth_colormap)
                if NIR:
                    cv2.imshow("Intel RealSense D435 NIR Left ", nir_lf_image)
                    cv2.imshow("Intel RealSense D435 NIR Right ", nir_rg_image)
            else:
                cv2.imshow("Camera ", image)
            if cv2.waitKey(1) == 27:
                break  # esc to quit
        cv2.destroyAllWindows()

    finally:
        # Stop streaming
        print("Closing camera device.")
        if rsFlag:
            pipeline.stop()
            del camera
        else:
            camera.stop()
Exemple #10
0
        # result = cv.bitwise_and(frame, frame, mask = mask)
        contours, _ = cv.findContours(mask, cv.RETR_TREE,
                                      cv.CHAIN_APPROX_SIMPLE)

        x = 0
        y = 0
        radius = 0

        if len(contours) > 0:
            c = max(contours, key=cv.contourArea)
            ((x, y), radius) = cv.minEnclosingCircle(c)
            M = cv.moments(c)

            if int(M["m00"]) > 0:

                rads = int(rw.read("setting/radius_bola.txt"))
                if radius > rads:
                    return ((int(x), int(y)))

        return (framecenter)


cap = WebcamVideoStream(0).start()
while True:
    frame = cap.read()

    obj = ObjCenter()
    print(obj.update(frame, (0, 0)))
print("exiting")
Exemple #11
0
from detect_text import detect_text

API_ENDPOINT = "http://159.89.172.250:4000/api/v1/vehicles/"

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))
net = cv2.dnn.readNetFromCaffe(
    "models/Car_Detect_Model/MobileNetSSD_deploy.prototxt.txt",
    "models/Car_Detect_Model/MobileNetSSD_deploy.caffemodel")
#Main_Stream:

cap1 = WebcamVideoStream(
    src="rtsp://*****:*****@121.6.207.205:8081/").start()

cap2 = WebcamVideoStream(
    src="rtsp://*****:*****@121.6.207.205:8083/").start()
''' Sub_Stream:
cap1=WebcamVideoStream(src="rtsp://*****:*****@121.6.207.205:8081/cam/realmonitor?channel=1&subtype=1").start()

cap2=WebcamVideoStream(src="rtsp://*****:*****@121.6.207.205:8083/cam/realmonitor?channel=1&subtype=1").start()
'''
lis = list()
lis1 = list()
lis2 = list()

j = 0
while True:
    image = cap1.read()
Exemple #12
0
    face_net.setInput(blob)
    detections = face_net.forward()
    for i in range(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence < 0.5:
            continue
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int32")
        dlibrect = dlib.rectangle(int(startX), int(startY), int(endX),
                                  int(endY))
    return dlibrect


(lx, ly) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rx, ry) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
file = WebcamVideoStream(src=0).start()
first_frame = file.read()
(iheight, iwidth) = first_frame.shape[:2]
print(iheight, iwidth)


def det_to_bb(det):
    x = det.left()
    y = det.top()
    w = det.right()
    h = det.bottom()
    return (x, y, w, h)


def video_detector(age_net):
    counter = 0
def init_streams():
    _out_streams = []
    _in_stream = WebcamVideoStream(src=config.in_stream).start()
    for stream in config.out_stream:
        _out_streams.append(WebcamVideoStream(src=stream).start())
    return _in_stream, _out_streams
Exemple #14
0
from time import time, sleep
import numpy as np
import cv2
import brickpi3
from statistics import median
from imutils.video import WebcamVideoStream, FPS

BP = brickpi3.BrickPi3()
cam = WebcamVideoStream(src=0).start()

fps = FPS().start()


def drive(left_motor, right_motor, max_speed=100, min_speed=100):
    #So maybe this wasn't the most "Functional" code.
    if right_motor < min_speed:
        right_motor = -min_speed
    elif left_motor < -min_speed:
        left_motor = -min_speed

    if left_motor > max_speed:
        left_motor = max_speed
    if right_motor > max_speed:
        right_motor = max_speed
    print('C:', -right_motor)
    print('B:', -left_motor)

    BP.set_motor_dps(BP.PORT_B, -left_motor)
    BP.set_motor_dps(BP.PORT_C, -right_motor)

        if tracker_type == 'BOOSTING':
            tracker = cv2.TrackerBoosting_create()
        if tracker_type == 'MIL':
            tracker = cv2.TrackerMIL_create()
        if tracker_type == 'KCF':
            tracker = cv2.TrackerKCF_create()
        if tracker_type == 'TLD':
            tracker = cv2.TrackerTLD_create()
        if tracker_type == 'MEDIANFLOW':
            tracker = cv2.TrackerMedianFlow_create()
        if tracker_type == 'GOTURN':
            tracker = cv2.TrackerGOTURN_create()

    # start video stream thread, allow buffer to fill
    print("[INFO] starting threaded video stream...")
    stream = WebcamVideoStream(src=0).start()  # default camera
    time.sleep(1.0)
    # first frame
    frame = stream.read()

    # Define an initial bounding box
    bbox = (287, 23, 86, 320)

    # Select object to track
    bbox = cv2.selectROI(frame, False)

    # Initialize tracker with first frame and bounding box
    ok = tracker.init(frame, bbox)

    while True:
        # grab next frame
Exemple #16
0

# create 2D gaussian kernel
def create_gaussian_kernel(kernel_size_X, kernel_size_Y, sigma_X, sigma_Y):
    gx = cv2.getGaussianKernel(kernel_size_X, sigma_X, cv2.CV_32F)
    gy = cv2.getGaussianKernel(kernel_size_Y, sigma_Y, cv2.CV_32F)
    return gx * gy.T


# init DoG kernel
g1 = create_gaussian_kernel(3, 3, 50, 50)
g2 = create_gaussian_kernel(3, 3, 0, 0)
DoG_kernel = g1 - g2

print("Camera init -> DONE")
cam = WebcamVideoStream(src=0).start()
print("Start DoG")

while True:
    # get current sensory state
    frame = cam.read()
    # grayscale
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # resize
    frame = cv2.resize(frame, (65, 65))
    # convolve
    frame = cv2.filter2D(frame, -1, DoG_kernel)
    # plot
    plt.imshow(frame)
    plt.pause(0.0000001)
    plt.draw()
Exemple #17
0
prototxt_path = os.path.join(path + 'model_data/deploy.prototxt')
caffemodel_path = os.path.join(path + 'model_data/weights.caffemodel')

net = cv2.dnn.readNetFromCaffe(prototxt_path,caffemodel_path)

# Start video 

print("Use default webcam? \n y/n : ")
cam= 0

if(input()=='n'):
    
    print("Enter video stream output link: ")
    cam = input()

video = WebcamVideoStream(cam).start()
time.sleep(1)

while(True):

    frame = video.read()
    frame = cv2.flip(frame,flipCode=1)
    ret=True
    if ret==True:
        
        (h, w) = frame.shape[:2]
        
        # Convert image to blob
        blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300,300)),1.0, (300,300), (104.0,177.0,123.0))
        net.setInput(blob)
        
import sys
import time
import numpy as np
from imutils.video import WebcamVideoStream
import imutils

#from matplotlib import pyplot as plt

def nothing(x):
    pass

# grab camera feeds
#capL = cv2.VideoCapture(1)
#capR = cv2.VideoCapture(0)

capL = WebcamVideoStream(src=1).start()
capR = WebcamVideoStream(src=0).start()


# initialize windows
#WINDOW_L1 = 'undistorted left cam'
#WINDOW_R1 = 'undistorted right cam'
cv2.namedWindow('Depth Map')
#cv2.namedWindow('Threshold')
#cv2.namedWindow('Shapes')
#cv2.namedWindow('Test')



# import calibration matrices
undistMapL = np.load('calibration/undistortion_map_left.npy')
Exemple #19
0
#######VARIABLES####################
##Aruco
ids_to_find = [129, 72]
marker_sizes = [40, 19]  #cm
marker_heights = [7, 4]
takeoff_height = 10
velocity = .5

aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_ARUCO_ORIGINAL)
parameters = aruco.DetectorParameters_create()
##

##Camera
horizontal_res = 640
vertical_res = 480
cap = WebcamVideoStream(src=0, width=horizontal_res,
                        height=vertical_res).start()

horizontal_fov = 62.2 * (math.pi / 180)  ##Pi cam V1: 53.5 V2: 62.2
vertical_fov = 48.8 * (math.pi / 180)  ##Pi cam V1: 41.41 V2: 48.8

##REQUIRED: Calibration files for camera
##Look up https://github.com/dronedojo/video2calibration for info
calib_path = "/home/pi/video2calibration/calibrationFiles/"

cameraMatrix = np.loadtxt(calib_path + 'cameraMatrix.txt', delimiter=',')
cameraDistortion = np.loadtxt(calib_path + 'cameraDistortion.txt',
                              delimiter=',')
#########

##Counters and script triggers
found_count = 0
# 800x600 windowed mode
#mon = {'top': 100, 'left': 2020, 'width': 1280, 'height': 720}
#mon = {'top': 0, 'left': 1920, 'width': 1280, 'height': 720}
mon = {'top': 0, 'left': 0, 'width': 1280, 'height': 720}
sct = None
if "screen" in url:
    sct = mss.mss()

webcam=False
#webcam=True
cap = None

if sct is None:
    if webcam:
        #cap = WebcamVideoStream(src=""+str(videoUrl)+"").start()
        cap = WebcamVideoStream(videoUrl).start()
    else:
        cap = cv2.VideoCapture(videoUrl)
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, procWidth)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, procHeight)

#cap = cv2.VideoCapture(videoUrl)

count=50
#skip=2000
skip=skipNr

SKIP_EVERY=150 #pick a frame every 5 seconds

count=1000000
#skip=0 #int(7622-5)
Exemple #21
0
def right_click(x, y):
    win32api.SetCursorPos((x, y))
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, x, y, 0, 0)


eye_cascade1 = cv2.CascadeClassifier('xml/haarcascade_mcs_eyepair_big.xml')
eye_cascade2 = cv2.CascadeClassifier('xml/haarcascade_mcs_eyepair_small.xml')
eye_cascade3 = cv2.CascadeClassifier('xml/haarcascade_eye_tree_eyeglasses.xml')
eye_cascade4 = cv2.CascadeClassifier('xml/haarcascade_eye.xml')

window_size = 5
windowX = []
windowY = []

cap = WebcamVideoStream(src=0).start()

mx = 0
my = 0

upper_mx = 0
lower_mx = 0

upper_my = 0
lower_my = 0

start_flag = True

while True:
    img = cap.read()
    img = cv2.flip(img, 1)
Exemple #22
0
    def __init__(self, camera=CAMERA, rotate=ROTATE):
        cams = [WebcamVideoStream(src=cam).start() for cam in camera]

        imgs = []
        for i, cam in enumerate(cams):
            # cam.set(cv2.CAP_PROP_BUFFERSIZE, 1) # Internal buffer will now store only x frames
            img = cam.read()
            imgs.append(img)

        image = mainhuman_activity.preprocess(imgs, rotate)

        # h, w, c = image_raw.shape
        # h2, w2, c2 = image2_raw.shape

        # print(h, w, c, h2, w2, c2)

        print("\n######################## Darknet")
        dark = dk.darknet_recog()
        print(dark.performDetect(image))

        print("\n######################## Openpose")
        opose = openpose_human(image)

        print("\n######################## LSTM")
        act = activity_human()

        # print("\n######################## Deepface")
        # dface = df.face_recog()
        # print(dface.run(image))

        print("\n######################## Facerec")
        facer = fr.face_recog(face_dir="./facerec/face/")

        act_labs = []
        act_confs = []

        # Main loop
        try:
            f = open(r'\\.\pipe\testing', 'r+b', 0)
            d = 0  # mode in communication
            alarmmode = False  # False mode deactive True mode active
            mode = True  # False normal mode True recognition mode
            security_threshold = 0.5
            face_tolerance = 0.6
            while True:
                # imgs = [mainhuman_activity.read2(cam) for cam in cams]
                n = struct.unpack('I', f.read(4))[0]  # Read str length
                s = f.read(n).decode('ascii')  # Read str
                f.seek(0)
                print('Accept from C#', s)
                if (s == 'AlarmDeactive'):
                    d = 7
                elif (s == 'AlarmActive'):
                    d = 6
                elif (s == 'FaceInput'):
                    d = 5
                elif (s == 'Normal'):
                    d = 4
                elif (s == 'Recognition'):
                    d = 3
                elif (s == 'Start'):
                    d = 2
                elif (s == 'Stop'):
                    d = 1
                elif (s == 'Received'):
                    d = 0
                else:
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                if (d == 7):
                    alarmmode = False  # False mode deactive True mode active
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                elif (d == 6):
                    alarmmode = True  # False mode deactive True mode active
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                elif (d == 5):
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                    n = struct.unpack('I', f.read(4))[0]  # Read str length
                    facename = f.read(n).decode('ascii')  # Read str
                    f.seek(0)
                    print('Accept from C#', facename)
                    imgs = []

                    img = cams[0].read()
                    imgs.append(img)

                    # for i, cam in enumerate(cams):
                    # # Decode the captured frames
                    # ret_val, img = cam.retrieve()
                    # imgs.append(img)

                    # Skip frame if there's nothing
                    if (imgs is [None]):
                        continue

                    image = mainhuman_activity.preprocess(imgs, rotate)
                    face_locs, face_names = facer.runinference(
                        image,
                        tolerance=face_tolerance,
                        prescale=0.25,
                        upsample=2)
                    # Facerec display
                    for (top, right, bottom,
                         left), face in zip(face_locs, face_names):
                        print(face)
                        if (face == "Unknown"):
                            bounds = [4 * left, 4 * top, 4 * right, 4 * bottom]
                            image = image[bounds[1]:bounds[3],
                                          bounds[0]:bounds[2]]
                    cv2.imwrite('facerec/face/' + facename + '.jpg', image)
                    print("\n######################## Facerec")
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                elif (d == 4):
                    mode = False  # False normal mode True recognition mode
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                elif (d == 3):
                    mode = True  # False normal mode True recognition mode
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                elif (d == 2):
                    for i, cam in enumerate(cams):
                        # cam.set(cv2.CAP_PROP_BUFFERSIZE, 1) # Internal buffer will now store only x frames
                        cam.stop()
                    camera = []
                    rotate = []
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                    n = struct.unpack('I', f.read(4))[0]  # Read str length
                    camnumber = f.read(n).decode('ascii')  # Read str
                    f.seek(0)
                    try:
                        cam_number = int(camnumber)
                    except ValueError:
                        pass
                    print('Accept from C#', camnumber)
                    for x in range(cam_number):
                        s = 'Go'
                        f.write(struct.pack('I', len(s)) +
                                s.encode('ascii'))  # Write str length and str
                        f.seek(0)
                        print('Sending to C#:', s)
                        n = struct.unpack('I', f.read(4))[0]  # Read str length
                        camtemp = f.read(n).decode('ascii')  # Read str
                        f.seek(0)
                        try:
                            camera.append(int(camtemp))
                            rotate.append(180)
                        except ValueError:
                            camera.append(camtemp)
                            rotate.append(180)
                            pass
                        print('Accept from C#', camtemp)
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                    n = struct.unpack('I', f.read(4))[0]  # Read str length
                    securitythresholdtemp = f.read(n).decode(
                        'ascii')  # Read str
                    f.seek(0)
                    if (securitythresholdtemp != " "):
                        try:
                            security_threshold = float(securitythresholdtemp)
                        except ValueError:
                            pass
                    print('Accept from C#', securitythresholdtemp)
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                    n = struct.unpack('I', f.read(4))[0]  # Read str length
                    facetolerancetemp = f.read(n).decode('ascii')  # Read str
                    f.seek(0)
                    if (facetolerancetemp != " "):
                        try:
                            face_tolerance = float(facetolerancetemp)
                        except ValueError:
                            pass
                    print('Accept from C#', facetolerancetemp)
                    cams = [
                        WebcamVideoStream(src=cam).start() for cam in camera
                    ]
                    s = 'Go'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                elif (d == 1):
                    s = 'Wait'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)
                elif (d == 0):
                    imgs = []
                    for i, cam in enumerate(cams):
                        # Grab the frames AND do the heavy preprocessing for each camera
                        # ret_val, img = cam.read()

                        # For better synchronization on multi-cam setup:
                        # Grab the frames first without doing the heavy stuffs (decode, demosaic, etc)
                        # ret_val = cam.grab()

                        # The FIFO nature of the buffer means we can't get the latest frame
                        # Thus skip the earlier frames. Delay stats: 7s 8fps +artifact >>> 2s 3fps
                        # for i in range(5):
                        # ret_val = cam.grab()

                        # Multi-threading using WebcamVideoStream
                        img = cam.read()
                        imgs.append(img)

                    # for i, cam in enumerate(cams):
                    # # Decode the captured frames
                    # ret_val, img = cam.retrieve()
                    # imgs.append(img)

                    # Skip frame if there's nothing
                    if (imgs is [None]):
                        continue

                    image = mainhuman_activity.preprocess(imgs, rotate)

                    print("\n######################## Openpose")
                    start_act, human_keypoints, humans = opose.runopenpose(
                        image)
                    # print(humans, human_keypoints)

                    print("\n######################## Darknet")
                    dobj = dark.performDetect(image)
                    print(dobj)

                    print("\n######################## Facerec")
                    face_locs, face_names = facer.runinference(
                        image,
                        tolerance=face_tolerance,
                        prescale=0.01,
                        upsample=1)
                    print(face_locs, face_names)

                    print("\n######################## LSTM")
                    print("Frame: %d/%d" % (opose.videostep, n_steps))
                    if start_act == True:
                        act_labs = []
                        act_confs = []
                        for key, human_keypoint in human_keypoints.items():
                            print(key, human_keypoint)
                            if (len(human_keypoint) == n_steps):
                                act.runinference(human_keypoint)
                                act_labs.append(act.action)
                                act_confs.append(act.conf)

                    print("\n######################## Display")
                    # opose.display_all(image, humans, act.action, act.conf, dobj, face_locs, face_names)
                    opose.display_all(image, humans, act_labs, act_confs, dobj,
                                      face_locs, face_names, mode)
                    s = 'Image'
                    f.write(struct.pack('I', len(s)) +
                            s.encode('ascii'))  # Write str length and str
                    f.seek(0)
                    print('Sending to C#:', s)

                if cv2.waitKey(1) == 27:
                    break

        except FileNotFoundError:
            raise
        cv2.destroyAllWindows()

        # print("FPS: ", opose.hisfps)
        fh = open("fps.txt", "w")
        for fps in opose.hisfps:
            fh.write("%.3f \n" % fps)
        fh.close()
Exemple #23
0
        print("TiltMove called: desired position is beneath lower limit" +
              '\n')
        desPosTilt = _TiltServoLL
    tiltDesPosQ.put(desPosTilt)
    tiltCurSpeedQ.put(speed)  # Send the new speed to the subprocess
    return


#Video Prep
camWandH = np.array([320, 240])  #set camera frame height and width here
camCenter = [(camWandH[0] / 2), (camWandH[1] / 2)]  #Ordered as Pan and Tilt
print("Center of screen is " + str(camCenter[0]) + ", " + str(camCenter[1]))
frontalface = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
profileface = cv2.CascadeClassifier("haarcascade_profileface.xml")
cv2.namedWindow('CreepyEye', cv2.WINDOW_NORMAL)
vs = WebcamVideoStream(
    src=0).start()  #If src is changed to a URL it will use that as the camera

while True:
    # Capture frame-by-frame
    frame = vs.read()
    # Our operations on the frame come here
    frame = imutils.resize(frame, width=camWandH[0])
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.flip(gray, 1, gray)  #	flip the image
    face = np.array(
        [0, 0, 0, 0]
    )  # This will hold the array that OpenCV returns when it finds a face: (makes a rectangle)
    Cface = np.array(
        [0,
         0])  # Center of the face: a point calculated from the above variable
    lastface = 0  # int 1-3 used to speed up detection. The script is looking for a right profile face,-
def main():
    # ---===--- Get the filename --- #
    '''filename = sg.popup_get_file('Filename to play')
    if filename:
        cap = cv2.VideoCapture(filename)
        #cap = WebcamVideoStream(src="http://192.168.0.101:8081").start()
    else:
        #vs = WebcamVideoStream(src=0).start()
        cap = cv2.VideoCapture(0)'''
    # ---===--- get stream --- #
    # cap = WebcamVideoStream(src=0).start()
    # cap = cv2.VideoCapture('actions1.mpg')
    # cap = WebcamVideoStream(src="http://192.168.0.100:8081").start()
    # ---===--- initialize --- #
    fps = FPS().start()
    count = 0
    count_th = 20
    rec = False
    sg.theme('Black')
    useVideo = False
    isFirst = True
    now = datetime.now()
    vodname = now.strftime("%m_%d_%Y,%H-%M-%S")

    cap = None
    # ---===--- define the window layout --- #
    tab1_layout = [
        [
            sg.T('Battery: '),
            sg.ProgressBar(1000, orientation='h', size=(10, 10),
                           key='battbar'),
            sg.Text('Last detected: '),
            sg.Text(' ', key='-detected-', size=(20, 1)),
            sg.Button('Capture', key='-STOP-'),
            sg.Button('EXIT', key='-EXIT-')
        ],
        [sg.Image(filename='', key='-image-')],
        [
            sg.Text('Height: ', size=(15, 1)),
            sg.Text('', size=(10, 1), justification='center', key='_HEIGHT_')
        ],
        [
            sg.Text('Latitude: ', size=(15, 1)),
            sg.Text('', size=(10, 1), justification='center', key='_LATI_')
        ],
        [
            sg.Text('Longitude: ', size=(15, 1)),
            sg.Text('', size=(10, 1), justification='center', key='_LONGTI_')
        ],
        [
            sg.Text('FPS: '),
            sg.Text(size=(15, 1), key='-FPS-'),
        ],
    ]

    column1 = [
        [
            sg.Text('Detected history',
                    background_color='#333333',
                    justification='center',
                    size=(20, 1)),
        ],
        #sg.Output(size=(40, 20))],
        [sg.Text('Gimbal command: '),
         sg.Text(size=(15, 1), key='-Gimbal-')]
    ]

    tab2_layout = [[sg.T('cropped/masked')],
                   [
                       sg.Image(filename='', key='-cropped-'),
                       sg.T(' ' * 30),
                       sg.Image(filename='', key='-masked-'),
                       sg.Column(column1, background_color='#333333')
                   ],
                   [
                       sg.Text('Size of the object: ', size=(15, 1)),
                       sg.Text('', size=(10, 1), justification='center'),
                       sg.Slider(range=(0, 500),
                                 default_value=15,
                                 size=(50, 10),
                                 orientation='h',
                                 key='-slider-')
                   ]]

    tab3_layout = [[sg.Image(filename='', key='-histp-')],
                   [sg.Text(' ', key='-history-', size=(20, 1))],
                   [
                       sg.Button('Next'),
                       sg.Button('Refresh'),
                       sg.Button('Prev')
                   ]]

    layout = [
        [sg.Text('DemodetectionUI', size=(15, 1), font='Helvetica 20')],
        [
            sg.TabGroup([[
                sg.Tab('Cam view', tab1_layout),
                sg.Tab('Area view', tab2_layout),
                sg.Tab('Detected view', tab3_layout)
            ]], )
        ],
    ]

    layoutWin2 = [
        [sg.Text('Tracking and DetectionDemo', key='-STATUS-')],
        # note must create a layout from scratch every time. No reuse
        [
            sg.Button('Start', key='-START-', disabled=True),
            sg.Button('Connect'),
            sg.Button('video'),
            sg.Checkbox('Record', key='-RECORD-', size=(12, 1), default=False)
        ],
        [sg.T(' ' * 12), sg.Button('Exit', )]
    ]

    # create the window and show it
    # main window
    window = sg.Window('DemoUI', layout, no_titlebar=False)
    # Open Connect Vods window
    window2 = sg.Window('DetectedHistory', layoutWin2, no_titlebar=False)

    # locate the elements we'll be updating. Does the search only 1 time
    image_elem = window['-image-']
    cropped_elem = window['-cropped-']
    masked_elem = window['-masked-']
    # initializing stuffs
    mcd = MCDWrapper()
    # subtractor = cv2.createBackgroundSubtractorMOG2(history = 50,varThreshold=50,detectShadows=True)
    object_bounding_box = None
    cropped = None
    count, fcount, fmeter = 0, 0, 0
    netip = '192.168.0.102'
    port = 25000
    out = None
    s = None
    win2_active = True
    showhist = False
    frame = None
    event2, values2 = None, None
    capp = os.listdir("cap")
    ptr = 1
    if capp:
        histpic = capp[len(capp) - ptr]

    while True:
        ev1, vals1 = window2.Read(timeout=100)
        if ev1 is None or ev1 == '-START-':
            window2.Close()
            win2_active = False
            if window2['-RECORD-'].Get():
                fourcc = cv2.VideoWriter_fourcc(*'XVID')
                out = cv2.VideoWriter('./recording/%s.avi' % (vodname), fourcc,
                                      20.0, (1020, 720))
            break
        if ev1 is None or ev1 == 'Exit':
            if out:
                out.release()
            break
        if ev1 is ev1 == 'video':
            filename = sg.popup_get_file('Filename to play')
            useVideo = True
            if filename:
                cap = cv2.VideoCapture(filename)
            else:
                cap = cv2.VideoCapture(0)
        if ev1 is ev1 == 'Connect':
            try:
                s = comm(netip, port)
                s = s.start()
                #cap = cv2.VideoCapture("http://192.168.0.102:8081")
                #streamer = ThreadedCamera("http://192.168.0.102:8081")
                cap = WebcamVideoStream(
                    src="http://192.168.0.102:8081").start()
                time.sleep(1)
            except:
                sg.popup('Error CameraIP not found')

        if not cap:
            window2['-START-'].update(disabled=True)
        else:
            if useVideo:
                captype = 'Video'
            else:
                captype = 'Aerial camera'
            window2['-STATUS-'].update(captype)
            window2['-START-'].update(disabled=False)
            # ---===--- LOOP through video file by frame --- #
    while True and win2_active == False:
        event, values = window.read(timeout=20)
        if event in ('-EXIT-', None):
            if s:
                s.stop()
            if out:
                out.release()
            break
        if useVideo == True:
            if frame is not None:
                lastframe = frame.copy()
            try:
                ret, frame = cap.read()
            except:
                frame = lastframe
        else:
            #frame = streamer.grab_frame()
            if frame is not None:
                lastframe = frame.copy()
            try:
                frame = cap.read()
            except:
                frame = None
                while i <= 5 or frame:
                    try:
                        cap = WebcamVideoStream(
                            src="http://192.168.0.102:8081").start()
                        time.sleep(1)
                        frame = cap.read()
                    except:
                        continue

                frame = lastframe
                i = 0

        now = datetime.now()
        s1 = now.strftime("%m_%d_%Y,%H-%M-%S")
        objsize = int(values['-slider-'])
        logging.basicConfig(filename='Detected.log',
                            format='%(asctime)s %(message)s',
                            datefmt='%m_%d_%Y,%H-%M-%S')
        data = "X0_0_0_0"
        if s:
            s.getxyhf()
            data = s.getxyh()
        split = data.split('_')

        if len(split) > 3 and split[0][0] == 'X':
            window['_LATI_'].update(split[0][1:])
            window['_LONGTI_'].update(split[1])
            window['_HEIGHT_'].update(split[2])
            window['battbar'].update_bar(int(float(split[3][0:2])))

        if frame is None:  # if out of data stop looping
            frame = lastframe
        if out:
            frameout = cv2.resize(frame, (1020, 720))
            out.write(frameout)

        # resizing the big pic
        # frame = cv2.resize(frame,(1280,500))
        # draw a bb around the obj

        # --------Event handler----------------
        if event == '-STOP-':  # stop video
            stop = window["-STOP-"]
            if frame.sum() > 0:
                object_bounding_box = cv2.selectROI("Frame",
                                                    frame,
                                                    fromCenter=False,
                                                    showCrosshair=True)
                object_tracker = cv2.TrackerMOSSE_create()
                object_tracker.init(frame, object_bounding_box)
                cv2.destroyAllWindows()
        if event == 'Refresh':
            capp = os.listdir("cap")
            ptr = 1
            if capp:
                histpic = capp[len(capp) - ptr]
                histtemp = cv2.imread("./cap/%s" % (histpic))
                histbytes = cv2.imencode('.png',
                                         histtemp)[1].tobytes()  # ditto
                histpic = histpic.replace('-', ':')
                window['-histp-'].update(data=histbytes)
                window['-history-'].update(histpic.replace('.jpg', ''))

        if event == 'Prev' and capp:
            if ptr <= len(capp):
                ptr += 1
            histpic = capp[len(capp) - ptr]
            histtemp = cv2.imread("./cap/%s" % (histpic))
            histbytes = cv2.imencode('.png', histtemp)[1].tobytes()  # ditto
            histpic = histpic.replace('-', ':')
            window['-histp-'].update(data=histbytes)
            window['-history-'].update(histpic.replace('.jpg', ''))

        if event == 'Next' and capp:
            if ptr > 1:
                ptr -= 1
            histpic = capp[len(capp) - ptr]
            histtemp = cv2.imread("./cap/%s" % (histpic))
            histbytes = cv2.imencode('.png', histtemp)[1].tobytes()  # ditto
            histpic = histpic.replace('-', ':')
            window['-histp-'].update(data=histbytes)
            window['-history-'].update(histpic.replace('.jpg', ''))
        '''if event == '-detected-':  # stop video
            capp = os.listdir("cap")
            tab3_layout = [[sg.T('Captured')], [sg.Listbox(values=capp,size=(20, 12), key='-LIST-')],
                        [sg.Text(' ',key ='-File-'), sg.Button('Select',size=(15, 1))]]
            winchoose = sg.Window('Detected History', tab3_layout)
            event2, values2 = winchoose.Read()
        if event2 is not None and event2 == 'Select':
            filechoosen = True
            if values2['-LIST-'] and filechoosen == True:
                filechoosen = str(values2['-LIST-'][0])
                winchoose.close()
                histtemp = cv2.imread("./cap/%s" % (filechoosen))
                histbytes = cv2.imencode('.png', frame)[1].tobytes()  # ditto
                winpic = sg.Window(filechoosen,[[sg.Image(data= histbytes, key='-histp-')],[sg.Button('Exit')]])
                eventp, valuesp = winpic.read()
                winpic['-histp-'].update(data=histbytes)
                if eventp == 'Exit':
                    filechoosen == False
                    winpic.close()'''

        # --------bgsubtraction-----------#
        if object_bounding_box is not None:
            success, object_bounding_box = object_tracker.update(frame)
            if success:
                (x, y, w, h) = [int(v) for v in object_bounding_box]
                cropped = frame[y:y + h, x:x + w].copy()
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                #detectsize = objsize/2
                cv2.rectangle(frame, (x + objsize, y + objsize),
                              (x + w - objsize, y + h - objsize), (0, 0, 255),
                              2)
                if (y + (h / 2) - 50) > frame.shape[0] / 2:  # order gimbal
                    if count >= count_th:
                        inp = "u"
                        if s:
                            s.setinp(inp)
                            s.sendcommand()
                        window['-Gimbal-'].update(inp)
                        print((y + h) / 2, frame.shape[0])
                        count = 0

                elif (y + (h / 2)) + 50 < frame.shape[0] / 2:
                    if count >= count_th:
                        inp = "d"
                        if s:
                            s.setinp(inp)
                            s.sendcommand()
                        print((y + h) / 2, frame.shape[0])

                        window['-Gimbal-'].update(inp)
                        count = 0
                else:
                    if count >= count_th:
                        inp = "s"
                        if s:
                            s.setinp(inp)
                            s.sendcommand()
                        print((y + h) / 2, frame.shape[0])

                        window['-Gimbal-'].update(inp)

                        count = 0
                count += 1

        if cropped is not None and cropped.sum() > 0:
            # resizing the small pic
            # cropped = cv2.resize(cropped,(600,400))
            # cv2.imwrite("./all/frame%d.jpg" % count, cropped)
            count += 1
            # mask = subtractor.apply(cropped)
            # for some reason the fast MCD accepts only the %4 size
            if cropped.shape[0] % 4 != 0 or cropped.shape[1] % 4 != 0:
                cropped = cv2.resize(cropped, ((cropped.shape[1] // 4) * 4,
                                               (cropped.shape[0] // 4) * 4))
            gray = cv2.cvtColor(cropped, cv2.COLOR_RGB2GRAY)
            mask = np.zeros(gray.shape, np.uint8)
            if (isFirst):
                mcd.init(gray)
                isFirst = False
            else:
                try:
                    mask = mcd.run(gray)
                except:
                    mcd.init(gray)

            # draw contours
            (cnts, _) = cv2.findContours(mask.copy(), cv2.RETR_TREE,
                                         cv2.CHAIN_APPROX_SIMPLE)
            for contour in cnts:
                if (cv2.contourArea(contour) > objsize):
                    cv2.drawContours(cropped, contour, -1, (0, 255, 0), 2)
                    (x, y, w, h) = cv2.boundingRect(contour)
                    cv2.rectangle(cropped, (x, y), (x + w, y + h), (0, 255, 0),
                                  3)
                    ##if cv2.contourArea(contour)>50:
                    # print(contour)
                    # cv2.drawContours(cropped,contour,-1,(0,255,0),2)
                    if x <= objsize / 2 or x >= cropped.shape[
                            1] - objsize / 2 or x + w <= objsize / 2 or x + w >= cropped.shape[
                                1] - objsize / 2:
                        if fmeter >= 3:
                            cv2.imwrite("./capc/%s.jpg" % (s1), cropped)
                            cv2.imwrite("./cap/%s.jpg" % (s1), frame)
                            logging.warning(" ")
                            fcount += 1
                            window['-detected-'].update(s1)
                            count += 1
                            fmeter = 0
                        else:
                            fmeter += 1
                    elif y <= objsize / 2 or y >= cropped.shape[
                            0] - objsize / 2 or y + h <= objsize / 2 or y + h >= cropped.shape[
                                0] - objsize / 2:
                        if fmeter >= 3:
                            cv2.imwrite("./capc/%s.jpg" % (s1), cropped)
                            cv2.imwrite("./cap/%s.jpg" % (s1), frame)
                            logging.warning(" ")
                            fcount += 1
                            window['-detected-'].update(s1)
                            count += 1
                            fmeter = 0
                        else:
                            fmeter += 1
        # showing
        imgbytes = cv2.imencode('.png', frame)[1].tobytes()  # ditto
        image_elem.update(data=imgbytes)
        fps.update()
        fps.stop()
        window['-FPS-'].update("{:.2f}".format(fps.fps()))

        if cropped is not None:
            try:
                # print(cropped.shape)
                imgcropped = cv2.imencode('.png', cropped)[1].tobytes()
                imgmarked = cv2.imencode('.png', mask)[1].tobytes()
                cropped_elem.update(data=imgcropped)
                masked_elem.update(data=imgmarked)
            except:
                continue
Exemple #25
0
import argparse
import imutils
import multiprocessing as mp
import cv2
import label_image
import os
#disable error. Maybe visit later
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#value for resizing image: Adjust and test stuff
size = 4
# testing speed stuff
cv2.setUseOptimized(False)
cv2.useOptimized()
# We load the xml file
classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
vs = WebcamVideoStream(src=0).start() #Using default WebCam connected to the PC.
#if vs = null then break gently...
#pool = mp.Pool(processes=mp.cpu_count())
while True:
    im = vs.read()
    # Flip camera
    im=cv2.flip(im,1,0)
    # Resize the image to speed up detection
    mini = cv2.resize(im, (int(im.shape[1]/size), int(im.shape[0]/size)))
    # detect MultiScale / faces 
    faces = classifier.detectMultiScale(mini)

    # Draw rectangles around each face
    for f in faces:
        (x, y, w, h) = [v * size for v in f] #Scale the shapesize backup
        cv2.rectangle(im, (x,y), (x+w,y+h), (255,255,255), 3)
Exemple #26
0
class ObjectDetection:
    def __init__(self, id):
        # self.cap = cv2.VideoCapture(id)
        self.cap = WebcamVideoStream(src=id).start()
        self.cfgfile = "cfg/yolov3.cfg"
        # self.cfgfile = 'cfg/yolov3-tiny.cfg'
        self.weightsfile = "yolov3.weights"
        # self.weightsfile = 'yolov3-tiny.weights'
        self.confidence = float(0.6)
        self.nms_thesh = float(0.8)
        self.num_classes = 1
        self.classes = load_classes('data/butt.names')
        self.colors = pkl.load(open("pallete", "rb"))
        self.model = Darknet(self.cfgfile)
        self.CUDA = torch.cuda.is_available()
        self.model.load_weights(self.weightsfile)
        self.model.net_info["height"] = 160
        self.inp_dim = int(self.model.net_info["height"])
        self.width = 1280  #640#1280
        self.height = 720  #360#720
        print("Loading network.....")
        if self.CUDA:
            self.model.cuda()
        print("Network successfully loaded")
        assert self.inp_dim % 32 == 0
        assert self.inp_dim > 32
        self.model.eval()

    def main(self):
        q = queue.Queue()
        while True:

            def frame_render(queue_from_cam):
                frame = self.cap.read(
                )  # If you capture stream using opencv (cv2.VideoCapture()) the use the following line
                # ret, frame = self.cap.read()
                frame = cv2.resize(frame, (self.width, self.height))
                queue_from_cam.put(frame)

            cam = threading.Thread(target=frame_render, args=(q, ))
            cam.start()
            cam.join()
            frame = q.get()
            q.task_done()
            fps = FPS().start()
            try:
                img, orig_im, dim = prep_image(frame, self.inp_dim)
                im_dim = torch.FloatTensor(dim).repeat(1, 2)
                if self.CUDA:  #### If you have a gpu properly installed then it will run on the gpu
                    im_dim = im_dim.cuda()
                    img = img.cuda()
                # with torch.no_grad():               #### Set the model in the evaluation mode
                output = self.model(Variable(img), self.CUDA)
                output = write_results(output,
                                       self.confidence,
                                       self.num_classes,
                                       nms=True,
                                       nms_conf=self.nms_thesh
                                       )  #### Localize the objects in a frame
                output = output.type(torch.half)

                if list(output.size()) == [1, 86]:
                    print(output.size())
                    pass
                else:
                    output[:,
                           1:5] = torch.clamp(output[:, 1:5], 0.0,
                                              float(
                                                  self.inp_dim)) / self.inp_dim

                    #            im_dim = im_dim.repeat(output.size(0), 1)
                    output[:, [1, 3]] *= frame.shape[1]
                    output[:, [2, 4]] *= frame.shape[0]
                    list(
                        map(
                            lambda boxes: write(boxes, frame, self.classes,
                                                self.colors), output))

            except:
                pass

            fps.update()
            fps.stop()
            ret, jpeg = cv2.imencode('.jpg', frame)
            print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
            print("[INFO] approx. FPS: {:.1f}".format(fps.fps()))

            return jpeg.tostring()
Exemple #27
0
def cv2_demo(net, transform, use_cuda):
    if use_spatial:
        tresh = 0.3  # UPDATE (smaller is more restrict)
    else:
        tresh = 0.25

    detected_objs = {}  # store by label name
    step = 0

    def predict(step, frame, detected_objs, obj_tracked):
        # - Also return the bbox of the tracked obj (2018.07.30)
        pt_tracked = [0, 0, 0, 0]

        k_reduce = 0.1  # bbox reduction for matching purpose (detected bbox is large...)
        pts = []
        # Start timer
        timer = cv2.getTickCount()

        height, width = frame.shape[:2]
        x = torch.from_numpy(transform(frame)[0]).permute(2, 0, 1)
        xx = Variable(x.unsqueeze(0))

        if use_cuda:
            xx = xx.cuda()
            y = net(xx)
        else:
            y = net(xx)  # forward pass

        detections = y.data  # Objects detected in the frame
        # scale each detection back up to the image
        scale = torch.Tensor([width, height, width, height])
        for i in range(detections.size(1)):
            j = 0
            while detections[0, i, j, 0] >= 0.6:
                pt = (detections[0, i, j, 1:] *
                      scale).cpu().numpy()  # Detection box
                # Store tracked obj bbox
                if labelmap[i - 1] == obj_tracked:
                    pt_tracked = pt

                # Check whether object was already detected
                if labelmap[i - 1] not in detected_objs:
                    # Add
                    vxy = [0, 0]  # assume static obj at beginning
                    detected_objs[labelmap[i - 1]] = [
                        myobj(time.time(), frame, 1, labelmap[i - 1],
                              get_bbox(pt, k_reduce), vxy)
                    ]
                    # Draw box
                    cv2.rectangle(
                        frame,
                        (int(pt[0]), int(pt[1])),  # UPPER LEFT
                        (int(pt[2]), int(pt[3])),  # LOWER RIGHT
                        COLORS[i % 3],
                        2)
                    cv2.putText(frame, labelmap[i - 1] + '(1)',
                                (int(pt[0]), int(pt[1])), FONT, 1,
                                (255, 255, 255), 1, cv2.LINE_AA)

                else:
                    if labelmap[i - 1] == 'person':
                        treshx = 0.5  # Less restrict for people
                    else:
                        treshx = tresh

                # Reduce bbox for figure matching
                    newobj = myobj(time.time(), frame, 0, labelmap[i - 1],
                                   get_bbox(pt, k_reduce), [0, 0])
                    #showme(newobj.get_img())

                    # Check it was detected before
                    #print('Comparing images...')
                    num, match_obj, dist = get_matching(
                        newobj, detected_objs[labelmap[i - 1]],
                        treshx)  # Return 0 if not in the list

                    if num != 0:  # object found --> estimate the spatial evidence
                        if use_spatial:
                            dt = newobj.get_time() - match_obj.get_time(
                            )  # Time diff
                            prior_x, prior_y = predict_pos(match_obj, dt)
                            likelihood_x, likelihood_y = newobj.get_p_xy()
                            x_distrib = update(prior_x, likelihood_x)
                            y_distrib = update(prior_y, likelihood_y)

                            x, y = newobj.get_xy()
                            approx_spatial_diff = abs(
                                x - x_distrib.mean) + abs(y - y_distrib.mean)
                            # Experimental
                            #spatial_evidence = 1 - approx_spatial_diff/300

                            if (
                                    dist + approx_spatial_diff / 300
                            ) < treshx:  # If spatial difference is small, it will keep
                                # udate velocity for the next iteration
                                vxy = []
                                vxy.append((newobj.get_xy()[0] -
                                            match_obj.get_xy()[0]) / dt)
                                vxy.append((newobj.get_xy()[1] -
                                            match_obj.get_xy()[1]) / dt)
                                newobj.set_vxy(vxy)  # set velocity
                                detected_objs[labelmap[i - 1]][
                                    match_obj.get_id() - 1] = newobj.set_id(
                                        num
                                    )  # Update with the matched newer object
                                print('-- dist(spatial):%2.2f(dt=%2.2fs)' %
                                      (approx_spatial_diff, dt))
                            else:
                                print('\n -- Too far!! New object distance:',
                                      approx_spatial_diff, ' --')
                                num = len(detected_objs[labelmap[
                                    i - 1]]) + 1  # Add as new image as well
                                detected_objs[labelmap[i - 1]].append(
                                    newobj.set_id(num))

                    else:  # Add if the same image was never seem before
                        num = len(detected_objs[labelmap[i - 1]]) + 1
                        detected_objs[labelmap[i - 1]].append(
                            newobj.set_id(num))
                        print(' -- New object added: %s...' %
                              (labelmap[i - 1]))

                    cv2.rectangle(
                        frame,
                        (int(pt[0]), int(pt[1])),  # UPPER LEFT
                        (int(pt[2]), int(pt[3])),  # LOWER RIGHT
                        COLORS[i % 3],
                        2)

                    # Print info
                    # Calculate Frames per second (FPS)
                    fps_current = cv2.getTickFrequency() / (
                        cv2.getTickCount() - timer)
                    score = detections[0, i, j, 0]
                    #cv2.putText(frame, labelmap[i - 1]+'('+str(num)+'): score:'+str(int(100* score)) +' (fps:'+ str(int(fps_current)) +')', (int(pt[0]), int(pt[1])),
                    #            FONT, 1, (255, 255, 255), 1, cv2.LINE_AA)
                    cv2.putText(
                        frame,
                        labelmap[i - 1] + ': score:' + str(int(100 * score)) +
                        ' (fps:' + str(int(fps_current)) + ')',
                        (int(pt[0]), int(pt[1])), FONT, 1, (255, 255, 255), 1,
                        cv2.LINE_AA)

                j += 1
                pts.append(pt)

        if not pts:
            print(' -- No detection (score < 60)...')

        return frame, pts, detected_objs, pt_tracked

    def detect_tracked(frame, bbox_tracked):
        obj_tracked = ''

        bb = bbox_tracked

        height, width = frame.shape[:2]
        x = torch.from_numpy(transform(frame)[0]).permute(2, 0, 1)
        xx = Variable(x.unsqueeze(0))

        if use_cuda:
            xx = xx.cuda()
            y = net(xx)
        else:
            y = net(xx)  # forward pass

        detections = y.data  # Objects detected in the frame
        # scale each detection back up to the image
        scale = torch.Tensor([width, height, width, height])
        for i in range(detections.size(1)):
            j = 0
            pt = (detections[0, i, j, 1:] *
                  scale).cpu().numpy()  # Detection box
            while detections[0, i, j, 0] >= 0.6:
                # Check whether bbox_tracked is within the detected obj
                if bb[0] > pt[0] and bb[1] > pt[1] and (
                        bb[0] + bb[2]) < pt[2] and (bb[1] + bb[3]) < pt[3]:
                    obj_tracked = labelmap[i - 1]

                j += 1

        if not obj_tracked:
            print(' -- Tracked object not detected (score < 60)...')
            obj_tracked = ''

        return obj_tracked

    # start video stream thread, allow buffer to fill
    print("[INFO] starting threaded video stream...")
    stream = WebcamVideoStream(src=0).start()  # default camera
    time.sleep(1.0)

    # grab frame for TDL selection
    frame = stream.read()
    # Select object to track
    bbox = cv2.selectROI(frame, False)

    # Define the tracker
    tracker = cv2.TrackerTLD_create()
    # Initialize tracker with first frame and bounding box
    ok = tracker.init(frame, bbox)

    # Identify tracked object
    obj_tracked = detect_tracked(frame, bbox)
    print('Tracked object:', obj_tracked)

    # start fps timer
    # loop over frames from the video file stream
    while True:
        step += 1
        # grab next frame
        frame = stream.read()
        key = cv2.waitKey(1) & 0xFF
        # update FPS counter
        fps.update()
        #frame0 = frame

        frame, pts, objs, pt_tracked = predict(step, frame, detected_objs,
                                               obj_tracked)
        print('Step:', step, end='')

        # Update tracker
        ok, bbox = tracker.update(frame)
        delta = 10  # delta to amplify bbox of detected obj

        # Draw bounding box
        if ok and bbox[0] > pt_tracked[0] - delta and bbox[
                1] > pt_tracked[1] - delta and (
                    bbox[0] + bbox[2]) < pt_tracked[2] + delta and (
                        bbox[1] + bbox[3]) < pt_tracked[3] + delta:
            # Tracking success
            p1 = (int(bbox[0]), int(bbox[1]))
            p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
            cv2.rectangle(frame, p1, p2, (0, 0, 255), 2, 1)
        else:
            # Tracking failure
            cv2.putText(frame, "Tracking failure detected", (100, 80),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

        # keybindings for display
        if key == ord('p'):  # pause
            while True:
                key2 = cv2.waitKey(1) or 0xff
                cv2.imshow('frame', frame)
                if key2 == ord('p'):  # resume
                    break
        cv2.imshow('frame', frame)
        if key == 27:  # Esc
            break
Exemple #28
0
class EyeCanSee(object):
    def __init__(self, center=int(cvsettings.CAMERA_WIDTH / 2), debug=False, is_usb_webcam=True, period_s=0.025):
        # Our video stream
        # If its not a usb webcam then get pi camera
        if not is_usb_webcam:
            self.vs = PiVideoStream(resolution=(cvsettings.CAMERA_WIDTH, cvsettings.CAMERA_HEIGHT))
            # Camera cvsettings
            self.vs.camera.shutter_speed = cvsettings.SHUTTER
            self.vs.camera.exposure_mode = cvsettings.EXPOSURE_MODE
            self.vs.camera.exposure_compensation = cvsettings.EXPOSURE_COMPENSATION
            self.vs.camera.awb_gains = cvsettings.AWB_GAINS
            self.vs.camera.awb_mode = cvsettings.AWB_MODE
            self.vs.camera.saturation = cvsettings.SATURATION
            self.vs.camera.rotation = cvsettings.ROTATION
            self.vs.camera.video_stabilization = cvsettings.VIDEO_STABALIZATION
            self.vs.camera.iso = cvsettings.ISO
            self.vs.camera.brightness = cvsettings.BRIGHTNESS
            self.vs.camera.contrast = cvsettings.CONTRAST

        # Else get the usb camera
        else:
            self.vs = WebcamVideoStream(src=0)
            self.vs.stream.set(cv2.CAP_PROP_FRAME_WIDTH, cvsettings.CAMERA_WIDTH)
            self.vs.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, cvsettings.CAMERA_HEIGHT)

        # Has camera started
        self.camera_started = False
        self.start_camera()  # Starts our camera

        # To calculate our error in positioning
        self.center = center

        # To determine if we actually detected lane or not
        self.detected_lane = False

        # debug mode on? (to display processed images)
        self.debug = debug

        # Time interval between in update (in ms)
        # FPS = 1/period_s
        self.period_s = period_s

        # Starting time
        self.start_time = time.time()

    # Mouse event handler for get_hsv
    def on_mouse(self, event, x, y, flag, param):
        if event == cv2.EVENT_LBUTTONDBLCLK:
            # Circle to indicate hsv location, and update frame
            cv2.circle(self.img_debug, (x, y), 3, (0, 0, 255))
            cv2.imshow('hsv_extractor', self.img_debug)

            # Print values
            values = self.hsv_frame[y, x]
            print('H:', values[0], '\tS:', values[1], '\tV:', values[2])

    def get_hsv(self):
        cv2.namedWindow('hsv_extractor')
        while True:
            self.grab_frame()

            # Bottom ROI
            cv2.rectangle(self.img_debug, (0, cvsettings.HEIGHT_PADDING_BOTTOM-2), (cvsettings.CAMERA_WIDTH, cvsettings.HEIGHT_PADDING_BOTTOM + cvsettings.IMG_ROI_HEIGHT + 2), (0, 250, 0), 2)

            # Top ROI
            cv2.rectangle(self.img_debug, (0, cvsettings.HEIGHT_PADDING_TOP-2), (cvsettings.CAMERA_WIDTH, cvsettings.HEIGHT_PADDING_TOP + cvsettings.IMG_ROI_HEIGHT + 2), (0, 250, 0), 2)

            # Object
            cv2.rectangle(self.img_debug, (0, cvsettings.OBJECT_HEIGHT_PADDING), (cvsettings.CAMERA_WIDTH, cvsettings.HEIGHT_PADDING_TOP - cvsettings.OBJECT_HEIGHT_PADDING), (238, 130, 238), 2)

            self.hsv_frame = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV)

            # Mouse handler
            cv2.setMouseCallback('hsv_extractor', self.on_mouse, 0)
            cv2.imshow('hsv_extractor', self.img_debug)

            key = cv2.waitKey(0) & 0xFF
            if key == ord('q'):
                break
        self.stop_camera()
        cv2.destroyAllWindows()

    # Starts camera (needs to be called before run)
    def start_camera(self):
        self.camera_started = True
        self.vs.start()
        time.sleep(2.0)  # Wait for camera to cool

    def stop_camera(self):
        self.camera_started = False
        self.vs.stop()

    # Grabs frame from camera
    def grab_frame(self):
        # Starts camera if it hasn't been started
        if not self.camera_started:
            self.start_camera()
        self.img = self.vs.read()
        self.img_debug = self.img.copy()

    # Normalizes our image
    def normalize_img(self):
        # Crop img and convert to hsv
        self.img_roi_bottom = np.copy(self.img[cvsettings.HEIGHT_PADDING_BOTTOM:int(cvsettings.HEIGHT_PADDING_BOTTOM + cvsettings.IMG_ROI_HEIGHT), :])
        self.img_roi_top = np.copy(self.img[cvsettings.HEIGHT_PADDING_TOP:int(cvsettings.HEIGHT_PADDING_TOP + cvsettings.IMG_ROI_HEIGHT), :])

        self.img_roi_bottom_hsv = cv2.cvtColor(self.img_roi_bottom, cv2.COLOR_BGR2HSV).copy()
        self.img_roi_top_hsv = cv2.cvtColor(self.img_roi_top, cv2.COLOR_BGR2HSV).copy()

        # Get our ROI's shape
        # Doesn't matter because both of them are the same shape
        self.roi_height, self.roi_width, self.roi_channels = self.img_roi_bottom.shape

    # Smooth image and convert to bianry image (threshold)
    # Filter out colors that are not within the RANGE value
    def filter_smooth_thres(self, RANGE, color):
        for (lower, upper) in RANGE:
            lower = np.array(lower, dtype='uint8')
            upper = np.array(upper, dtype='uint8')

            mask_bottom = cv2.inRange(self.img_roi_bottom_hsv, lower, upper)
            mask_top = cv2.inRange(self.img_roi_top_hsv, lower, upper)

        blurred_bottom = cv2.medianBlur(mask_bottom, 5)
        blurred_top = cv2.medianBlur(mask_top, 5)

        # Morphological transformation
        kernel = np.ones((2, 2), np.uint8)
        smoothen_bottom = blurred_bottom #cv2.morphologyEx(blurred, cv2.MORPH_OPEN, kernel, iterations=5)
        smoothen_top = blurred_top  # cv2.morphologyEx(blurred, cv2.MORPH_OPEN, kernel, iterations=5)

        """
        if self.debug:
            cv2.imshow('mask bottom ' + color, mask_bottom)
            cv2.imshow('blurred bottom' + color, blurred_bottom)

            cv2.imshow('mask top ' + color, mask_top)
            cv2.imshow('blurred top' + color, blurred_top)
        """

        return smoothen_bottom, smoothen_top

    # Gets metadata from our contours
    def get_contour_metadata(self):

        # Metadata (x,y,w,h)for our ROI
        contour_metadata = {}
        for cur_img in [self.thres_yellow_bottom, self.thres_yellow_top, self.thres_blue_bottom, self.thres_blue_top]:
            key = ''

            # Blue is left lane, Yellow is right lane
            if cur_img is self.thres_yellow_bottom:
                key = 'right_bottom'
            elif cur_img is self.thres_yellow_top:
                key = 'right_top'
            elif cur_img is self.thres_blue_bottom:
                key = 'left_bottom'
            elif cur_img is self.thres_blue_top:
                key = 'left_top'

            _, contours, hierarchy = cv2.findContours(cur_img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

            cur_height, cur_width = cur_img.shape

            # Get index of largest contour
            try:
                areas = [cv2.contourArea(c) for c in contours]
                max_index = np.argmax(areas)
                cnt = contours[max_index]

                # Metadata of contour
                x, y, w, h = cv2.boundingRect(cnt)

                # Normalize it to the original picture
                x += int(cvsettings.WIDTH_PADDING + w / 2)

                if 'top' in key:
                    y += int(cvsettings.HEIGHT_PADDING_TOP +h /2)
                else:
                    y += int(cvsettings.HEIGHT_PADDING_BOTTOM + h / 2)

                contour_metadata[key] = (x, y)

                self.detected_lane = True

            # If it throws an error then it doesn't have a ROI
            # Means we're too far off to the left or right
            except:

                # Blue is left lane, Yellow is right lane
                x = int(cvsettings.WIDTH_PADDING) - cvsettings.CAMERA_WIDTH

                if 'bottom' in key:
                    y = int(cur_height / 2) + int(cvsettings.HEIGHT_PADDING_BOTTOM + cur_height / 2)
                else:
                    y = int(cur_height / 2) + int(cvsettings.HEIGHT_PADDING_TOP + cur_height / 2)

                if 'right' in key:
                    x = int(cur_width)*2

                contour_metadata[key] = (x, y)

                self.detected_lane = False

        return contour_metadata

    # Gets the centered coord of the detected lines
    def get_centered_coord(self):
        bottom_centered_coord = None
        top_centered_coord = None

        left_xy_bottom = self.contour_metadata['left_bottom']
        right_xy_bottom = self.contour_metadata['right_bottom']

        left_xy_top = self.contour_metadata['left_top']
        right_xy_top = self.contour_metadata['right_top']

        bottom_xy = (left_xy_bottom[0] + right_xy_bottom[0], left_xy_bottom[1] + right_xy_bottom[1])
        bottom_centered_coord = (int(bottom_xy[0] / 2), int(bottom_xy[1] / 2))

        top_xy = (left_xy_top[0] + right_xy_top[0], left_xy_top[1] + right_xy_top[1])
        top_centered_coord = (int(top_xy[0] / 2), int(top_xy[1] / 2))

        # Left can't be greater than right and vice-versa
        if left_xy_top > right_xy_top:
            top_centered_coord = (0, top_centered_coord[1])
        elif right_xy_top < left_xy_top:
            top_centered_corrd = (cvsettings.CAMERA_WIDTH, top_centered_coord[1])

        if left_xy_bottom > right_xy_bottom:
            bottom_centered_coord = (0, bottom_centered_coord[1])
        elif right_xy_bottom < left_xy_bottom:
            bottom_centered_coord = (cvsettings.CAMERA_WIDTH, top_centered_coord[1])

        return bottom_centered_coord, top_centered_coord

    # Gets the error of the centered coordinates (x)
    # Also normlizes their values
    def get_errors(self):
        top_error = (float(self.center_coord_top[0]) - float(self.center))/float(cvsettings.CAMERA_WIDTH + cvsettings.WIDTH_PADDING)
        bottom_error = (float(self.center_coord_bottom[0]) - float(self.center))/float(cvsettings.CAMERA_WIDTH + cvsettings.WIDTH_PADDING)
        relative_error = (float(self.center_coord_top[0]) - float(self.center_coord_bottom[0]))/float(cvsettings.CAMERA_WIDTH + cvsettings.WIDTH_PADDING)

        return (top_error + relative_error + bottom_error)/3.0

    # Object avoidance
    def where_object_be(self):
        # We only want to detect objects in our path: center of top region and bottom region
        # So to save processing speed, we'll only threshold from center of top region to center of bottom region
        left_x = cvsettings.WIDTH_PADDING
        right_x = cvsettings.CAMERA_WIDTH

        # Image region with objects
        img_roi_object = self.img[cvsettings.OBJECT_HEIGHT_PADDING : int(cvsettings.HEIGHT_PADDING_TOP - cvsettings.OBJECT_HEIGHT_PADDING), left_x:right_x]
        img_roi_object_hsv = cv2.cvtColor(img_roi_object, cv2.COLOR_BGR2HSV).copy()

        # Filtering color and blurring
        for (lower, upper) in cvsettings.OBJECT_HSV_RANGE:
            lower = np.array(lower, dtype='uint8')
            upper = np.array(upper, dtype='uint8')

            mask_object = cv2.inRange(img_roi_object_hsv, lower, upper)

        blurred_object = cv2.medianBlur(mask_object, 25)

        # Finding position of object (if its there)
        _, contours, hierarchy = cv2.findContours(blurred_object.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        left_x = self.contour_metadata['left_top'][0]
        right_x = self.contour_metadata['right_top'][0]

        for c in contours:
            areas = cv2.contourArea(c)

            # Object needs to be larger than a certain area
            if areas > cvsettings.OBJECT_AREA:
                x, y, w, h = cv2.boundingRect(c)

                y += int(cvsettings.OBJECT_HEIGHT_PADDING + h / 2)

                # Confusing part - this finds the object and makes it think that
                # it is also a line to avoid bumping into it
                # It +w and -w to find which line its closest to and then set
                # the opposite as to be the new left/right lane
                # e.g. line is closest to left lane (x-w), so left lane new x is
                # (x+w)

                distance_to_left = abs(x - left_x)
                distance_to_right = abs(x+w - right_x)

                # Make object's left most area the middle of right lane
                if distance_to_left > distance_to_right:
                    self.contour_metadata['right_top'] = (x, self.contour_metadata['right_top'][1])

                # Make object's right most area the middle of left lane
                elif distance_to_right > distance_to_right:
                    self.contour_metadata['left_top'] = (x+w, self.contour_metadata['left_top'][1])

                if self.debug:
                    cv2.circle(self.img_debug, (x+(w/2), y+(h/2)), 5, (240, 32, 160), 2)

        if self.debug:
            cv2.imshow('Blurred object', blurred_object)


    # Where are we relative to our lane
    def where_lane_be(self):
        # Running once every period_ms
        while float(time.time() - self.start_time) < self.period_s:
            pass

        # Update time instance
        self.start_time = time.time()

        # Camera grab frame and normalize it
        self.grab_frame()
        self.normalize_img()

        # Filter out them colors
        self.thres_blue_bottom, self.thres_blue_top = self.filter_smooth_thres(cvsettings.BLUE_HSV_RANGE, 'blue')
        self.thres_yellow_bottom, self.thres_yellow_top = self.filter_smooth_thres(cvsettings.YELLOW_HSV_RANGE, 'yellow')

        # Get contour meta data
        self.contour_metadata = self.get_contour_metadata()

        # Finds objects and (and corrects lane position)
        # this overwrite contour_metadata
        #self.where_object_be()

        # Find the center of the lanes (bottom and top) [we wanna be in between]
        self.center_coord_bottom, self.center_coord_top = self.get_centered_coord()

        # Gets relative error between top center and bottom center
        self.relative_error = self.get_errors()

        if self.debug:
            # Drawing locations
            blue_top_xy = self.contour_metadata['left_top']
            blue_bottom_xy = self.contour_metadata['left_bottom']
            yellow_top_xy = self.contour_metadata['right_top']
            yellow_bottom_xy = self.contour_metadata['right_bottom']

            # Circles to indicate lanes
            cv2.circle(self.img_debug, blue_top_xy, 5, (255, 0, 0), 2)
            cv2.circle(self.img_debug, blue_bottom_xy, 5, (255, 0, 0), 2)
            cv2.circle(self.img_debug, yellow_top_xy, 5, (0, 255, 255), 2)
            cv2.circle(self.img_debug, yellow_bottom_xy, 5, (0, 255, 255), 2)
            cv2.circle(self.img_debug, self.center_coord_bottom, 5, (0, 255, 0), 3)
            cv2.circle(self.img_debug, self.center_coord_top, 5, (0, 255, 0), 3)

            # ROI for object detection
            cv2.rectangle(self.img_debug, (0, cvsettings.OBJECT_HEIGHT_PADDING), (cvsettings.CAMERA_WIDTH, cvsettings.HEIGHT_PADDING_TOP - cvsettings.OBJECT_HEIGHT_PADDING), (238, 130, 238), 2)

            # Displaying image
            cv2.imshow('img', self.img_debug)
            #cv2.imshow('img_roi top', self.img_roi_top)
            #cv2.imshow('img_roi bottom', self.img_roi_bottom)
            #cv2.imshow('img_hsv', self.img_roi_hsv)
            cv2.imshow('thres_blue_bottom', self.thres_blue_bottom)
            cv2.imshow('thres_blue_top', self.thres_blue_top)
            cv2.imshow('thres_yellow_bottom', self.thres_yellow_bottom)
            cv2.imshow('thres_yellow_top', self.thres_yellow_top)
            key = cv2.waitKey(0) & 0xFF  # Change 1 to 0 to pause between frames

    # Use this to calculate fps
    def calculate_fps(self, frames_no=100):
        fps = FPS().start()

        # Don't wanna display window
        if self.debug:
            self.debug = not self.debug

        for i in range(0, frames_no):
            self.where_lane_be()
            fps.update()

        fps.stop()

        # Don't wanna display window
        if not self.debug:
            self.debug = not self.debug

        print('Time taken: {:.2f}'.format(fps.elapsed()))
        print('~ FPS : {:.2f}'.format(fps.fps()))

    # Use this to save images to a location
    def save_images(self, dirname='dump'):
        import os
        img_no = 1

        # Makes the directory
        if not os.path.exists('./' + dirname):
            os.mkdir(dirname)

        while True:
            self.grab_frame()

            if self.debug:
                cv2.imshow('frame', self.img)

            k = cv2.waitKey(1) & 0xFF

            if k == ord('s'):
                cv2.imwrite(os.path.join(dirname, 'dump_' + str(img_no) + '.jpg'), self.img)
                img_no += 1

            elif k == ord('q'):
                break

        cv2.destroyAllWindows()
    # Destructor
    def __del__(self):
        self.vs.stop()
        cv2.destroyAllWindows()
Exemple #29
0
import threading
import cv2
from PIL import Image
from werkzeug.utils import secure_filename

outputFrame = None
lock = threading.Lock()

app = Flask(__name__)

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
uploads_path = os.path.join(APP_ROOT, 'datasets')
allowed_set = set(['png', 'jpg', 'jpeg'])
app.secret_key = os.urandom(24)

vs = WebcamVideoStream(src=0).start()
time.sleep(2.0)

faceObj = FaceObject()

@app.route('/')
def index_page():
    return render_template(template_name_or_list='index.html')


def detect_face_live():

    global outputFrame,vs, lock

    while True:
Exemple #30
0
from imutils.video import WebcamVideoStream
import time


def triggered(message):
    url = 'https://bosch-ville-api.unificationengine.com/v1/message/send'
    api_token = 'Y2gmZGV2aWNlX3R5cGU9WERJ'
    headers = {'Content-Type': 'application/json', 'Authorization': api_token}
    body = {"phone_number": "+6590698810", "message": message}
    requests.post(url, data=json.dumps(body), headers=headers)


#this is the cascade we just made. Call what you want
wrist_cascade = cv2.CascadeClassifier('wrist_cascade.xml')

vs = WebcamVideoStream(src=0).start()
time.sleep(1)
fps = FPS().start()

while fps._numFrames < 10000:
    #capture frame-by-frame
    img = vs.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    wrist = wrist_cascade.detectMultiScale(gray, 50, 50)

    for (x, y, w, h) in wrist:
        #place a rectangle around object detected
        print(wrist)
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        #font = cv2. FONT_HERSHEY_SIMPLEX
Exemple #31
0
def Camera(source=0):
    return WebcamVideoStream(source).start()
Exemple #32
0
trackers = []
trackableObjects = {}

# Load the model
#net = cv2.dnn.readNet('models/mobilenet-ssd/FP16/mobilenet-ssd.xml', 'models/mobilenet-ssd/FP16/mobilenet-ssd.bin')
net = cv2.dnn.readNetFromCaffe("models/MobileNetSSD_deploy.prototxt",
                               "models/MobileNetSSD_deploy.caffemodel")

# Specify target device
#net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)

# if a video path was not supplied, grab a reference to the webcam
if not args.get("input", False):
    print("[INFO] starting video stream...")
    #vs = VideoStream(src=0).start()
    vs = WebcamVideoStream(src=0).start()
    time.sleep(2.0)

# otherwise, grab a reference to the video file
else:
    print("[INFO] opening video file...")
    vs = cv2.VideoCapture(args["input"])

time.sleep(1)
fps = FPS().start()

# loop over frames from the video file stream
while True:
    try:
        # grab the frame from the threaded video stream
        # make a copy of the frame and resize it for display/video purposes
Exemple #33
0
import numpy as np
import argparse
import imutils
import time
import cv2

from utils.fps2 import FPS2

import dlib

from trackers.camshifttracker import CAMShiftTracker

if __name__ == '__main__':

    print("[info] starting to read a webcam ...")
    capWebCam = WebcamVideoStream(0).start()
    time.sleep(1.0)

    # initialize dlib face detector

    frontFaceDetector = dlib.get_frontal_face_detector()

    # meanShift tracker

    camShifTracker = None

    curWindow = None

    # start the frame per second  (FPS) counter
    #fps = FPS2().start()
Exemple #34
0
from imutils.object_detection import non_max_suppression
from imutils.video import WebcamVideoStream
import imutils
import cv2
import numpy

vs = WebcamVideoStream(src=0).start()
SIZE_W = 300

while(True):
	frame = vs.read()
	cv2.imshow('Video',imutils.resize(frame, width=min(SIZE_W, frame.shape[1])))
	if cv2.waitKey(10) & 0xFF == ord('q'):
                break

Exemple #35
0
class MyPhoto(threading.Thread):
    def __init__(self, img_root, stream_type, pi_ip_address, listen_port,
                 debug):
        threading.Thread.__init__(self)
        print("Initializing MyPhoto class")
        self.img_root = img_root
        self.debug = debug
        self.img_root_date = os.path.join(self.img_root,
                                          datetime.now().strftime("%Y-%m-%d"))
        self.pi_ip_address = pi_ip_address
        self.listen_port = listen_port
        #self.bad_img_transfers = 0
        self.stream_type = stream_type
        self.video_status = False
        self.img_checked = False
        self.img_seconds = [str(x).zfill(2) for x in range(0, 60)]
        # self.create_root_img_dir() ## this action is strictly duplicated in MyClient
        self.connect_to_video()
        self.start()
        self.total_missing = 0
        self.per_min_missing = []
        self.ten_missing = False
        self.img_checker_thread = threading.Thread(target=self.img_checker,
                                                   daemon=True)
        self.img_checker_thread.start()

    # comment this function out
    # def create_message(self, to_send):
    #     """
    #     Configure the message to send to the server.
    #     Elements are separated by a carriage return and newline.
    #     The first line is always the datetime of client request.

    #     param: to_send <class 'list'>
    #             List of elements to send to server.

    #     return: <class 'bytes'> a byte string (b''), ready to
    #             send over a socket connection
    #     """
    #     dt_str = [datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")]
    #     for item in to_send:
    #         dt_str.append(item)

    #     message = '\r\n'.join(dt_str)
    #     logging.log(25, "Sending Message: \n{}".format(message))
    #     return message.encode()

    def my_recv_all(self, s, timeout=2):
        """
        Regardless of message size, ensure that entire message is received
        from server.  Timeout specifies time to wait for additional socket
        stream.

        param: s <class 'socket.socket'>
                A socket connection to server.
        return: <class 'str'>
                A string containing all info sent.
        """
        # try:
        # make socket non blocking
        s.setblocking(0)

        # total data partwise in an array
        total_data = []
        data = ''

        # beginning time
        begin = time.time()
        while 1:
            # if you got some data, then break after timeout
            if total_data and time.time() - begin > timeout:
                break

            # if you got no data at all, wait a little longer, twice the timeout
            elif time.time() - begin > timeout * 2:
                break

            # recv something
            try:
                data = s.recv(8192).decode()
                if data:
                    total_data.append(data)
                    # change the beginning time for measurement
                    begin = time.time()
                else:
                    # sleep for sometime to indicate a gap
                    time.sleep(0.1)
            except:
                pass

        # join all parts to make final string
        return ''.join(total_data)

    def has_correct_files(self):
        ''' Image Check Files'''
        logging.info('Running image checker has_correct_files')
        t = datetime.now() - timedelta(minutes=1)
        d = t.strftime("%Y-%m-%d")
        hr = t.strftime("%H%M")

        self.prev_min_img_dir = os.path.join(self.img_root_date,
                                             t.strftime("%H%M"))
        should_have_files = [
            os.path.join(self.prev_min_img_dir,
                         '{} {}{}_photo.png'.format(d, hr, s))
            for s in self.img_seconds
        ]
        has_files = [
            os.path.join(self.prev_min_img_dir, f)
            for f in os.listdir(self.prev_min_img_dir) if f.endswith('.png')
        ]

        missing = list(set(should_have_files) - set(has_files))
        num_missing_now = len(missing)
        if self.debug:
            logging.info('Number of images missing this minute: {}'.format(
                num_missing_now))
            print('images missing this min: {} files'.format(num_missing_now))
            print('images missing these files: {}'.format(missing))

        if len(missing) > 0:
            #self.bad_img_transfers += 1
            self.total_missing += num_missing_now
            logging.warning(
                'images missing this min: {} files'.format(num_missing_now))
            logging.warning('images missing these files: {}'.format(missing))

        if len(missing) > 3:
            self.per_min_missing.append((hr, num_missing_now))
        """ restarts if 3 or more image files 
        are missing for more than 10 minutes in the last 15 minutes"""

        if self.ten_missing == False:
            if len(self.per_min_missing) > 10:
                self.ten_missing = True
                self.time_missing = datetime.now()
                self.missing_now = len(self.per_min_missing)
                logging.critical(
                    'first check: self.per_min_missing = {} at {}'.format(
                        self.missing_now,
                        self.time_missing.strftime("%H:%M:%S")))
        else:
            if datetime.now() > self.time_missing + timedelta(minutes=5):
                if len(self.per_min_missing) > int(self.missing_now + 1):
                    logging.critical(
                        'self.total_image_missing = {}.  Next line runs os._exit(1)'
                        .format(self.total_missing))
                    os._exit(1)
                else:
                    self.ten_missing = False
                    logging.info(
                        'Resetting per_min_missing. Time is: {}. Number missing is: {}'
                        .format(datetime.now().strftime('%H:%M:%S'),
                                len(self.per_min_missing)))
                    self.per_min_missing = []

            else:
                logging.critical(
                    'More than 10 images missing: {} at {}. Total missing minutes = {}'
                    .format(len(missing), hr, len(self.per_min_missing)))

        # if first_check:
        #     first_check = False

        time.sleep(30)

    def create_root_img_dir(self):
        if not os.path.isdir(self.img_root):
            os.makedirs(self.img_root)

    def connect_to_video(self):
        self.video_status = False
        # Select the stream type based on that specified in server.conf
        if self.stream_type == "mjpeg":
            stream_path = "http://" + self.pi_ip_address + ":8080/stream/video.mjpeg"
        elif self.stream_type == "h264":
            stream_path = "http://" + self.pi_ip_address + ":8080/stream/video.h264"
        elif self.stream_type == "jpeg":
            stream_path = "http://" + self.pi_ip_address + ":8080/stream/video.jpeg"

        # Attempt to start the video stream
        self.cam = WebcamVideoStream(stream_path).start()

        self.img_restart_attempts = 0
        # Keep attempting to open the video stream until it is opened
        while not self.cam.stream.isOpened():
            self.cam = WebcamVideoStream(stream_path).start()
            self.video_status = False
            self.img_restart_attempts += 1
            logging.warning("Unable to connect to video")
            if self.debug:
                print('Unable to connect to video')
            if self.img_restart_attempts >= 5:
                # self.restart_dat_img()
                # subprocess.run("sudo reboot", shell = True)
                # subprocess.run("sudo service uv4l_raspicam restart", shell = True)
                # time.sleep(5)
                self.img_restart_attempts = 0
                # subprocess.run("sudo service hpd_mobile restart", shell = True)

            time.sleep(10)

        # Set the video status to true
        self.video_status = True
        logging.info("Connected to video stream")
        if self.debug:
            print('Connected to video stream')

    def img_dir_update(self):
        # This function is run in a separate thread to continuously create a new directory for each day, and for each minute.
        while 1:
            date_dir = os.path.join(self.img_root,
                                    datetime.now().strftime("%Y-%m-%d"))
            if not os.path.isdir(date_dir):
                os.makedirs(date_dir)
            self.img_root_date = date_dir

            min_dir = os.path.join(self.img_root_date,
                                   datetime.now().strftime("%H%M"))
            if not os.path.isdir(min_dir):
                os.makedirs(min_dir)
            self.img_dir = min_dir

    def img_checker(self):
        while True:
            try:
                if datetime.now().second == 1 and not self.img_checked:
                    if self.debug:
                        logging.info('Checking images captured')
                    # file_checker = threading.Thread(target=self.has_correct_files)
                    # file_checker.start()
                    self.has_correct_files()
                    self.img_checked = True
            except Exception as e:
                logging.warning(
                    'Error with img_checker. Exception: {}'.format(e))
            if datetime.now().second != 1:
                self.img_checked = False

    def run(self):
        dir_create = threading.Thread(target=self.img_dir_update)
        dir_create.start()

        # Wait for self.img_dir to exist
        time.sleep(1)
        while True:
            # if self.bad_img_transfers >= 5:
            #     try:
            #         self.cam.stop()
            #     except Exception as e:
            #         logging.warning(
            #             'Unable to close cam.  Potentially closed.  Exception: {}'.format(e))
            #     finally:
            #         self.cam.stop()
            #     self.bad_img_transfers = 0
            #     self.connect_to_video()
            f_name = datetime.now().strftime("%Y-%m-%d %H%M%S_photo.png")
            f_path = os.path.join(self.img_dir, f_name)

            # Only capture a photo if it doesn't already exist
            if not os.path.isfile(f_path):
                if not len(os.listdir(self.img_dir)) >= 60:
                    img = False
                    img = self.cam.read()
                    if type(img) is not np.ndarray:
                        logging.warning(
                            'Camera read did not return image.  Attempting to reconnect to video'
                        )
                        if self.debug:
                            print(
                                'Camera read did not return image.  Attempting to restart video connection'
                            )
                        try:
                            self.cam.stop()
                        except Exception as e:
                            logging.warning(
                                'Unable to close cam.  Potentially closed.  Exception: {}'
                                .format(e))
                        finally:
                            self.cam.stop()
                        self.connect_to_video()

                    elif type(img) is np.ndarray:
                        try:
                            # Convert image to greyscale
                            img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

                            # Write to disk
                            # This is closing the image file???
                            # look into /var/log/messages grep for oom killer
                            cv2.imwrite(f_path, img)
                            if datetime.now().second == 0:
                                logging.info("Created file: {}".format(f_path))

                        except Exception as e:
                            logging.warning(
                                "Unable to convert to grayscale and write to disk.  Error: {}.  File: {}\tAttempting to restart video connection"
                                .format(e, f_name))
                            if self.debug:
                                print(
                                    "Unable to convert to grayscale and write to disk.  Error: {}.  File: {}\tAttempting to restart video connection"
                                    .format(e, f_name))
                            try:
                                self.cam.stop()
                            except Exception as e:
                                logging.warning(
                                    'Unable to close cam.  Potentially closed.  Exception: {}'
                                    .format(e))
                            self.connect_to_video()
class Stereo_Vision:
    """Class for obtaining and analyzing depth maps"""

    def __init__(self, cam_L_src, cam_R_src, display_frames=True, threshold=110):

        # initialize camera feed threads
        self.capL = WebcamVideoStream(src=cam_L_src).start()
        time.sleep(0.5)
        self.capR = WebcamVideoStream(src=cam_R_src).start()

        self.stop = False

        self.display_frames = display_frames

        # initialize windows
        if self.display_frames == True:
            cv2.namedWindow('Depth Map')
            cv2.namedWindow('Threshold')
            cv2.namedWindow('Shapes')

        # import calibration matrices
        self.undistMapL = np.load('calibration/undistortion_map_left.npy')
        self.undistMapR = np.load('calibration/undistortion_map_right.npy')
        self.rectMapL = np.load('calibration/rectification_map_left.npy')
        self.rectMapR = np.load('calibration/rectification_map_right.npy')

        # initialize blank frames
        self.rectL = np.zeros((640, 480, 3), np.uint8)
        self.rectR = np.zeros((640, 480, 3), np.uint8)

        self.quadrant_near_object = np.zeros((3, 3), dtype=bool)
        self.threshold = threshold

        self.exec_time_sum = 0
        self.frame_counter = 0
        self.fps = 0

        self.no_object_left_column = False
        self.no_object_mid_column = False
        self.no_object_right_column = False
        self.no_object_bottom_row = False
        self.no_object_mid_row = False
        self.no_object_top_row = False

    def start(self):
        self.vision_thread = Thread(target=self.start_stereo)
        self.vision_thread.start()

    def stop_vision(self):
        self.stop = True

    def get_quadrants(self):
        return self.quadrant_near_object

    def start_stereo(self):
        while (self.stop != True):

            self.start_time = time.time()
            self.frameL = self.capL.read()
            self.frameR = self.capR.read()

            # remap cameras to remove distortion
            self.rectL = cv2.remap(self.frameL, self.undistMapL, self.rectMapL, cv2.INTER_LINEAR)
            self.rectR = cv2.remap(self.frameR, self.undistMapR, self.rectMapR, cv2.INTER_LINEAR)

            # convert rectified from RGB to 8 bit grayscale
            self.grayRectL = cv2.cvtColor(self.rectL, cv2.COLOR_BGR2GRAY)
            self.grayRectR = cv2.cvtColor(self.rectR, cv2.COLOR_BGR2GRAY)
            self.grayFrameL = cv2.cvtColor(self.frameL, cv2.COLOR_BGR2GRAY)
            self.grayFrameR = cv2.cvtColor(self.frameR, cv2.COLOR_BGR2GRAY)

            # create depth map

            self.object_left_column = True
            self.object_mid_column = True
            self.object_right_column = True
            self.object_top_row = True
            self.object_mid_row = True
            self.object_bottom_row = True

            self.stereo = cv2.StereoBM_create(numDisparities=0, blockSize=13)

            self.disparity = self.stereo.compute(self.grayRectL, self.grayRectR).astype(np.float32)
            self.disparity = cv2.normalize(self.disparity, self.disparity, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX,
                                           dtype=cv2.CV_8UC1)

            # blur depth map to filter high frequency noise
            self.disparity_blur = cv2.medianBlur(self.disparity, 13)

            #apply 2 pixel border, helps keep contours continuous at extreme edges of image
            self.disparity_blur = cv2.copyMakeBorder(self.disparity_blur, top=2, bottom=2, right=2, left=2,
                                                     borderType=cv2.BORDER_CONSTANT, value=0)

            # threshold depth map
            self.disparity_thresh_imm = cv2.threshold(self.disparity_blur, self.threshold, 255, cv2.THRESH_BINARY)[1]

            self.disparity_edge = cv2.Canny(self.disparity_thresh_imm, 100, 200)

            # contour finding
            self.contours = cv2.findContours(self.disparity_edge, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[1]

            self.disparity_contours = np.zeros((480, 640, 3), np.uint8)

            for self.contour_index in self.contours:
                #draw bounding rectangle around each contour
                self.rect = cv2.minAreaRect(self.contour_index)
                self.box = cv2.boxPoints(self.rect)
                self.box = np.int0(self.box)

                #calculate area of each box
                self.box_area = abs(self.box[2][0] - self.box[0][0]) * abs(self.box[2][1] - self.box[0][1])
                self.col = -1
                self.row = -1

                if self.box_area > 1000: #pixels^2
                    cv2.drawContours(self.disparity_contours, [self.box], 0, (0, 255, 0), 2)  # draw green box

                    # determine which quadrants the rectangle occupies
                    for corner_index in range(0, 3):
                        self.col = self.box[corner_index][0] / 213  # x coordinates
                        self.row = self.box[corner_index][1] / 160  # y coordinates
                        if self.row > 2:
                            self.row = 2

                        if self.col > 2:
                            self.col = 2

                        if self.col != -1 and self.row != -1:
                            self.quadrant_near_object[self.row][self.col] = True


            #Boolean logic to set flags for main program
            if (self.quadrant_near_object[0][0] == False and self.quadrant_near_object[1][0] == False or
                        self.quadrant_near_object[2][0] == False):
                self.no_object_left_column = True
            else:
                self.no_object_left_column = False

            if (self.quadrant_near_object[0][1] == False and self.quadrant_near_object[1][1] == False and
                        self.quadrant_near_object[2][1] == False):
                self.no_object_mid_column = True
            else:
                self.no_object_mid_column = False

            if (self.quadrant_near_object[0][2] == False and self.quadrant_near_object[1][2] == False and
                        self.quadrant_near_object[2][2] == False):
                self.no_object_right_column = True
            else:
                self.no_object_right_column = False

            if (self.quadrant_near_object[0][0] == False and self.quadrant_near_object[0][1] == False and
                        self.quadrant_near_object[0][2] == False):
                self.no_object_top_row = True
            else:
                self.no_object_top_row = False
            if (self.quadrant_near_object[1][0] == False and self.quadrant_near_object[1][1] == False and
                        self.quadrant_near_object[1][2] == False):
                self.no_object_mid_row = True
            else:
                self.no_object_mid_row = False

            if (self.quadrant_near_object[2][0] == False and self.quadrant_near_object[2][1] == False and
                        self.quadrant_near_object[2][2] == False):
                self.no_object_bottom_row = True
            else:
                self.no_object_bottom_row = False

            #FPS calculations
            self.end_time = time.time()
            self.exec_time = self.end_time - self.start_time
            self.exec_time_sum += self.exec_time
            self.frame_counter += 1
            self.fps = 1.0 / self.exec_time

            #draw edges in pink
            cv2.drawContours(self.disparity_contours, self.contours, -1, (180, 105, 255), -1)
            cv2.line(self.disparity_contours, (0,160), (640,160), (255,0,0))
            cv2.line(self.disparity_contours, (0,320), (640,320), (255,0,0))
            cv2.line(self.disparity_contours, (213,0), (213,480), (255,0,0))
            cv2.line(self.disparity_contours, (426,0), (426,480), (255,0,0))


            if self.display_frames == True:
                cv2.imshow('Threshold', self.disparity_edge)
                cv2.imshow('Depth Map', self.disparity)
                cv2.imshow('Shapes', self.disparity_contours)

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

        self.capL.stop()
        self.capR.stop()
        cv2.destroyAllWindows()

    def save_frames(self, filename_index):
        cv2.imwrite("Images/DepthMap" + str(filename_index) + ".jpg", self.disparity)
        cv2.imwrite("Images/DepthMapBlur" + str(filename_index) + ".jpg", self.disparity_blur)
        cv2.imwrite("Images/Contours_" + str(filename_index) + ".jpg", self.disparity_contours)
        cv2.imwrite("Images/LeftCam" + str(filename_index) + ".jpg", self.frameL)
        cv2.imwrite("Images/RightCam" + str(filename_index) + ".jpg", self.frameR)
Exemple #37
0
	def __init__(self, camid,camname,camlocation,cam_type,threadID, name, counter):
		threading.Thread.__init__(self)
		self.threadID = threadID
		self.name = name
		self.counter = counter

		# Getting camera details
		self.camname = camname
		self.camid = camid
		self.camlocation = camlocation
		cam_type = cam_type

		self.face_cascade = cv2.CascadeClassifier('./models/haarcascades/haarcascade_frontalface_default.xml')
		self.font= cv2.FONT_HERSHEY_SIMPLEX
		self.fileDir = os.path.dirname(os.path.realpath(__file__))
		self.unknownusers_dir = os.path.join(self.fileDir, "img/unknownusers")
		self.classifierModel = os.path.join(self.fileDir, 'ginilib/openface/generated-embeddings/classifier.pkl')
		if os.path.exists(self.classifierModel):
			with open(self.classifierModel, 'rb') as f:
				if sys.version_info[0] < 3:
					(self.le, self.clf) = pickle.load(f)
				else:
					(self.le, self.clf) = pickle.load(f, encoding='latin1')
		dbs=dbops()
		# create a directory for camera
		settingdetails = dbs.get_settingsdetails()
		rootpathprimary = settingdetails[0][2]
		rootpathsecondary = settingdetails[0][3]
		rootpathtertiary = settingdetails[0][4]
		if os.path.exists(rootpathprimary):
			pvideoroot = rootpathprimary
		else:
			self.createdir(rootpathprimary)
			pvideoroot = rootpathprimary

		if os.path.exists(rootpathsecondary):
			svideoroot = rootpathsecondary
		else:
			self.createdir(rootpathsecondary)
			svideoroot = rootpathsecondary

		if os.path.exists(rootpathtertiary):
			tvideoroot = rootpathtertiary
		else:
			self.createdir(rootpathtertiary)
			tvideoroot = rootpathtertiary

		camdir = pvideoroot+"/videos/" + camname
		self.base = pvideoroot+"/videos/"
		self.root = self.base + camname + "/live"
		self.root_hist = self.base + camname + "/hist"
		self.createdir(camdir)
		self.createdir(self.base)
		self.createdir(self.root)
		self.createdir(self.root_hist)

		framerate = 18.0
		self.fps = FPS().start()

		self.Camtype=cam_type
		if self.Camtype=="webcam":
			try:
					ur =int(self.camid)
					self.video = WebcamVideoStream(src=ur).start()
			except Exception as e:
				print(e)
				print("Unable to open",camname)
		elif self.Camtype == "rtsp":
			try:
					ur = self.camid
					self.video = VideoStream(ur).start()
			except Exception as e:
				# print(e)
				print("Unable to open", camname)
		elif self.Camtype=="http":
			try:
				self.ur = camid
				self.imgResp = urllib.request(self.ur)
				# self.imgResp= requests.get(self.ur,stream=True,verify=True)

			except Exception as e:
				#print(e)
				print("Unable to open",camname)
		elif self.Camtype=="pi":
			try:
				# camera = PiCamera()
				# rawCapture = PiRGBArray(camera)
				# allow the camera to warmup
				time.sleep(0.1)
			except Exception as e:
				#print(e)
				print("Unable to open",camname)
def main():
	points = []
	ticks =str(int(time.time() * 1000))
	# construct the argument parse and parse the arguments
	ap = argparse.ArgumentParser()
	ap.add_argument("-v", "--video",
		help="path to the (optional) video file")
	ap.add_argument("-b", "--buffer", type=int, default=64,
		help="max buffer size")
	args = vars(ap.parse_args())

	# define the lower and upper boundaries of the "green"
	# ball in the HSV color space, then initialize the
	# list of tracked points

	# BLUE
	ballLower, ballUpper = getBallColor()

	pts = deque(maxlen=args["buffer"])

	# created a *threaded *video stream, allow the camera senor to warmup,
	# and start the FPS counter
	debugPrint("[INFO] sampling THREADED frames from webcam...")
	vs = WebcamVideoStream(src=1).start()
	frame = vs.read()
	if frame is None:
		debugPrint("Changed to default camera!")
		# Take the default camera (webcam is not connected)
		vs = WebcamVideoStream(src=0).start()

	if __ENABLE_VIDEO_OUT__:
		outStream = cv2.VideoWriter('Output/output' + ticks +'.avi', -1, 20.0, (1000,705))

	eventHook = EventHook()

	# keep looping
	while True:
		# grab the current frame
		frame = vs.read()

		if frame is None:
			continue
		
		# resize the frame, blur it, and convert it to the HSV
		# color space
		frame = imutils.resize(frame, width=1000)
		frame = frame[40:745,0:1000]

		# blurred = cv2.GaussianBlur(frame, (11, 11), 0)
		hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

		# construct a mask for the color "green", then perform
		# a series of dilations and erosions to remove any small
		# blobs left in the mask
		mask = cv2.inRange(hsv, ballLower, ballUpper)
		mask = cv2.erode(mask, None, iterations=2)
		mask = cv2.dilate(mask, None, iterations=2)

		# find contours in the mask and initialize the current
		# (x, y) center of the ball
		cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
			cv2.CHAIN_APPROX_SIMPLE)[-2]
		center = None

		# only proceed if at least one contour was found
		if len(cnts) > 0:
			# find the largest contour in the mask, then use
			# it to compute the minimum enclosing circle and
			# centroid
			c = max(cnts, key=cv2.contourArea)
			((x, y), radius) = cv2.minEnclosingCircle(c)
			M = cv2.moments(c)
			center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

			# only proceed if the radius meets a minimum size
			if radius > 5: #Changed from 10
				# draw the circle and centroid on the frame,
				# then update the list of tracked points
				cv2.circle(frame, (int(x), int(y)), int(radius),
					(0, 255, 255), 2)
				cv2.circle(frame, center, 5, (0, 0, 255), -1)

		# update the points queue
		pts.appendleft(center)

		debugPrint(center)
		if center is None:
			points.append((-1,-1))
		else:
			points.append(center)

		# loop over the set of tracked points
		for i in xrange(1, len(pts)):
			# if either of the tracked points are None, ignore
			# them
			if pts[i - 1] is None or pts[i] is None:
				continue

			# otherwise, compute the thickness of the line and
			# draw the connecting lines
			thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5)
			cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)

		# show the frame to our screen
		cv2.imshow("Frame", frame)
		
		if __ENABLE_VIDEO_OUT__:
			outStream.write(frame)

		# add replay frame
		addReplayFrame(frame)
		
		key = cv2.waitKey(1) & 0xFF

		# if the 'q' key is pressed, stop the loop
		if key == ord("q"):
			break

		if center is None:
			if eventHook.fire([(-1,-1)]):
				saveGoalVideo()
		else:
			normalizePoint = (int(2000*float(center[0]/1000.0)),int(1000*float(center[1]/705.0)))
			if eventHook.fire([normalizePoint]):
				saveGoalVideo()

			debugPrint(normalizePoint)

	# cleanup the camera and close any open windows
	cv2.destroyAllWindows()
	vs.stop()
	savePtsVector(points,ticks)
	raise SystemExit
	if __ENABLE_VIDEO_OUT__:
		outStream.release()
Exemple #39
0
ap.add_argument("-i", "--input", default=int(0), help="path to input")
ap.add_argument("-o", "--output", default=int(0), help="path to output")
ap.add_argument("-f",
                "--file",
                required=False,
                default=None,
                help="path to output file txt")
args = vars(ap.parse_args())

# initialize the QRdecoder object
qr = QRdecoder()

# created a *threaded* video stream, allow the camera sensor to warmup,
# and start the FPS counter
if args["mode"] == 0:
    vs = WebcamVideoStream(args["input"]).start()
    time.sleep(2.0)
else:
    vs = FileVideoStream(args["input"]).start()

#fourcc = cv2.VideoWriter_fourcc(*'mp4v')

resolution = (640, 480)
frame_rate = 20
#rec = cv2.VideoWriter(args["output"], fourcc, frame_rate, resolution)

print("[INFO] THREADED frames initialized...")
fps = FPS().start()

# loop over some frames...this time using the threaded stream
while True:
host = args["IPaddress"]
port = args["PORT"]

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.connect((host, port))
s.bind((host, port))
print ("UDP socket bound to %s" %(port))

#initialize the video stream and allow the camera sensor to warmup
print("Warming up camera")

if args["setting"]:
    os.system('qv4l2')

vs1 = WebcamVideoStream(src=1).start()
vs2 = WebcamVideoStream(src=2).start()

time.sleep(2.0)

#initialize the FourCC, video writer, dimensions of the frame, and zeros array
fps = args["fps"]
frame1 = vs1.read()
frame1 = imutils.resize(frame1, args["width"])
(h, w) = frame1.shape[:2]
print(h, w)
numframe = int(fps * args['length'] * 60)
print(numframe)
ts = None
toverflow = 0
record = False
Exemple #41
0
class App:

    def __init__(self, logger, src, ROOT_DIR):
        self.vs = WebcamVideoStream(src)
        self.fps = FPS()
        self.logger = logger
        self.ROOT_DIR = ROOT_DIR

        cv2.namedWindow("Webcam")
        cv2.namedWindow("roi")
        cv2.namedWindow("stacked")
        cv2.createTrackbar('dilate kernel', 'roi', 3, 5, self.none)
        cv2.createTrackbar('erode kernel', 'roi', 2, 5, self.none)
        cv2.createTrackbar('blackhat kernel', 'roi', 21, 30, self.none)

        self.mouse = Mouse(window="Webcam")
        self.gt = Graphics()
        # self.hist = Hist()
        self.msg = "draw a rectangle to continue ..."
        self.font_20 = ImageFont.truetype(f'{self.ROOT_DIR}/fonts/raleway/Raleway-Light.ttf', 20)
        self.font_10 = ImageFont.truetype(f'{self.ROOT_DIR}/fonts/raleway/Raleway-Light.ttf', 15)
        self.font_30 = ImageFont.truetype(f'{self.ROOT_DIR}/fonts/raleway/Raleway-Light.ttf', 30)
        self.font_40 = ImageFont.truetype(f'{self.ROOT_DIR}/fonts/raleway/Raleway-Medium.ttf', 50)
        # self.stabilizer = VidStab()
        self.predictor = parser_v3.Predictor(model_path=f'{self.ROOT_DIR}/models/model_0.1v7.h5', root_dir=self.ROOT_DIR)

    def run(self):
        self.vs.start()
        self.fps.start()
        self.logger.info("app started ...")
        frame = self.vs.read()
        wh, ww, _ = frame.shape
        cv2.moveWindow("Webcam", 0, 0)
        cv2.moveWindow("roi", ww, 0)
        cv2.moveWindow("stacked", 0, wh + 79)
        self.mouse.rect = ((200, 200), (ww - 200, wh - 200))

        while True:
            frame = self.vs.read()
            # frame = frame[:, ::-1]  # flip
            orig = frame.copy()
            # stabilized_frame = self.stabilizer.stabilize_frame(
            #     input_frame=frame, smoothing_window=30, border_size=50)

            if self.mouse.rect:
                p1, p2 = self.mouse.rect
                roi = self.gt.draw_roi(orig, p1, p2)
                if roi.size != 0:

                    gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)

                    dk = cv2.getTrackbarPos("dilate kernel", "roi")
                    ek = cv2.getTrackbarPos("erode kernel", "roi")
                    bk = cv2.getTrackbarPos("blackhat kernel", "roi")
                    thresh = im.thresify(gray_roi, dk, ek, bk)
                    # print(thresh.shape)

                    # overlap thresh on original image
                    mod_thres = cv2.bitwise_not(thresh)
                    #orig[p1[1]:p2[1], p1[0]:p2[0]] = cv2.merge((mod_thres, mod_thres, mod_thres))
                    # boxes, digits, cnts = component.get_symbols(
                    #     thresh, im=roi, draw_contours=True)

                    digits, boxes = component.connect_cnts2(thresh, roi)
                    stacked_digits, resized_digits = component.stack_digits(digits, im.resize)
                    res, latex_image, labels = self.predictor.parse(resized_digits, boxes)

                    # annotate symbols
                    # for label, (pre_label, x_min, y_min, x_max, y_max) in zip(labels, boxes):
                    #     self.gt.write(orig, label, (x_min, y_min), self.font_20)

                    # res = self.predictor.parse(resized_digits, boxes)
                    #print(f"res: {res}")

                    self.gt.write(orig, res, (10, wh - wh / 8), self.font_10)

                    # self.gt.draw_boxes(orig, boxes, p1, p2)

                    # self.hist.draw(gray_roi)
                    cv2.imshow('roi', thresh)
                    cv2.imshow('stacked', stacked_digits)
                    # cv2.imshow('latex_image', latex_image)

                # self.gt.write(orig, self.msg, (10, 10), self.font_20)
            else:
                orig[:, :] = cv2.blur(orig, (100, 100))
                windowRect = cv2.getWindowImageRect('Webcam')
                wx, wy, ww, wh = windowRect
                self.gt.write(orig, self.msg, (100, wh / 2 - 30), self.font_30)

            cv2.imshow('Webcam', orig)
            # cv2.imshow('stab', stabilized_frame)
            self.fps.update()

            key = cv2.waitKey(1)

            if (key & 0xFF == ord('q')) or (key & 0xFF == 27):
                self.logger.info('exiting ...')
                break

            elif key & 0xFF == ord('c'):
                CAPTURED_DIR = f'{self.ROOT_DIR}/screenshots'
                imageName = f'{CAPTURED_DIR}/{str(time.strftime("%Y_%m_%d_%H_%M"))}.png'
                cv2.imwrite(imageName, orig)
                self.logger.info(f'screenshot saved at {imageName}')

        self.fps.stop()
        self.vs.stop()
        cv2.destroyAllWindows()
        self.logger.info("elapsed time: {:.2f}".format(self.fps.elapsed()))
        self.logger.info("approx. FPS: {:.2f}".format(self.fps.fps()))

    def none(self, x):
        pass