Пример #1
0
 def createWindow(self):
     cv2.namedWindow(self._windowName)#创建视屏窗口
     self._isWindowCreated = True#修改类属性,说明已经创建窗口,为Ture
Пример #2
0
import numpy as np
from cv2 import cv2 as cv

def nada(x):
    pass

#criação de uma imagem preta e de uma janela
img = np.zeros((300,512,3), np.uint8)
cv.namedWindow('image')

#criação das trackbar para troca de cor. O último parâmetro é uma função que executa toda vez que algo muda
cv.createTrackbar('R','image',0,255,nada)
cv.createTrackbar('G','image',0,255,nada)
cv.createTrackbar('B','image',0,255,nada)

# criação de um "interruptor" que liga e desliga a funcionalidade
switch = '0 : OFF \n1 : ON'
cv.createTrackbar(switch, 'image',0,1,nada)
while(1):
    cv.imshow('image',img)
    k = cv.waitKey(1) & 0xFF
    if k == 27:
        break
    # pega a posição atual das quatro trackbar
    r = cv.getTrackbarPos('R','image')
    g = cv.getTrackbarPos('G','image')
    b = cv.getTrackbarPos('B','image')
    s = cv.getTrackbarPos(switch,'image')
    if s == 0:
        img[:] = 0 #seta a imagem toda preta
    else:
Пример #3
0
from cv2 import cv2 as cv
src = cv.imread('foreward.jpeg')
cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE)
cv.imshow('input_image', src)
cv.waitKey(0)
cv.destroyAllWindows()
def main():
    button_pin = 7
    GPIO.setmode(GPIO.BCM)
    # load our serialized model from disk
    print("[INFO] loading model...")
    net = cv2.dnn.readNetFromCaffe(prototxt, model)

    # initialize the video stream, allow the cammera sensor to warmup,
    # and initialize the FPS counter
    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    fps = FPS().start()

    ultrasonic = UltrasonicSystem({1: [8, 25], 2: [24, 23], 3: [15, 14]}, 3)
    ultrasonic.add_sensors()
    ultrasonic_spawn = threading.Thread(target=ultrasonic.spawn_sensor_threads,
                                        daemon=True)
    ultrasonic_spawn.start()
    GPIO.add_event_detect(button_pin, GPIO.RISING)
    try:
        # loop over the frames from the video stream
        while True:
            # grab the frame from the threaded video stream and resize it
            # to have a maximum width of 500 pixels
            frame = vs.read()
            frame = cv2.flip(frame, 1)
            # grab the frame dimensions and convert it to a blob
            (h, w) = frame.shape[:2]
            blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
                                         0.007843, (300, 300), 127.5)

            # pass the blob through the network and obtain the detections and
            # predictions
            net.setInput(blob)
            detections = net.forward()

            # loop over the detections
            for i in np.arange(0, detections.shape[2]):
                # extract the confidence (i.e., probability) associated with
                # the prediction
                confidence = detections[0, 0, i, 2]

                # filter out weak detections by ensuring the `confidence` is
                # greater than the minimum confidence
                if confidence > confidence_threshold:
                    # extract the index of the class label from the
                    # `detections`, then compute the (x, y)-coordinates of
                    # the bounding box for the object
                    idx = int(detections[0, 0, i, 1])
                    box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                    (startX, startY, endX, endY) = box.astype("int")

                    # draw the prediction on the frame
                    label = "{}: {:.2f}%".format(CLASSES[idx],
                                                 confidence * 100)
                    cv2.rectangle(frame, (startX, startY), (endX, endY),
                                  COLORS[idx], 2)
                    y = startY - 15 if startY - 15 > 15 else startY + 15
                    cv2.putText(frame, label, (startX, y),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
            frame = ultrasonic.write_measurements_to_frame(frame)
            # show the output frame
            cv2.namedWindow("Frame", cv2.WND_PROP_FULLSCREEN)
            cv2.setWindowProperty("Frame", cv2.WND_PROP_FULLSCREEN,
                                  cv2.WINDOW_FULLSCREEN)
            # cv2.resizeWindow("Frame", 640, 480)
            cv2.imshow("Frame", frame)
            key = cv2.waitKey(1) & 0xFF

            if GPIO.event_detected(button_pin):
                ultrasonic.stop = True
                time.sleep(1)
                break

            # update the FPS counter
            fps.update()
        cleanup(fps, vs)
    except KeyboardInterrupt:
        cleanup(fps, vs)
drawing = False  # true if mouse is pressed
ix, iy = -1, -1


def draw_shape(event, x, y, flags, param):  # mouse callback func
    global ix, iy, drawing
    if event == cv.EVENT_LBUTTONDOWN:
        drawing = True
        ix, iy = x, y

    elif event == cv.EVENT_MOUSEMOVE:
        if drawing == True:
            cv.rectangle(img, (ix, iy), (x, y), (0, 255, 0), 0)

    elif event == cv.EVENT_LBUTTONUP:
        drawing = False
        cv.rectangle(img, (ix, iy), (x, y), (0, 255, 0),
                     2)  # if thickness -1 = fill
        cv.imshow("Image", img)


img = np.zeros((512, 512, 3), np.uint8)

while True:
    cv.namedWindow("Image", cv.WINDOW_NORMAL)
    cv.imshow("Image", img)
    cv.setMouseCallback("Image", draw_shape)
    cv.waitKey(0)
    cv.destroyAllWindows()
Пример #6
0
    dst = cv.divide(m1, m2)
    cv.imshow("divide_demo", dst)


def multiply_demo(m1, m2):  # 两张图片需要同样大
    dst = cv.multiply(m1, m2)
    cv.imshow("multiply_demo", dst)


def logic_demo(m1, m2):
    dst = cv.bitwise_and(m1, m2)
    cv.imshow("logic_demo", dst)


def contrast_brightness_demo(image, c, b):
    h, w, ch = image.shape
    blank = np.zeros([h, w, ch], image.dtype)
    dst = cv.addWeighted(image, c, blank, 1 - c, b)
    cv.imshow("con-bri-demo", dst)


src1 = cv.imread("")
src2 = cv.imread("")
cv.namedWindow("image1", cv.WINDOW_AUTOSIZE)
cv.imshow("image1", src1)
cv.imshow("image2", src2)
add_demo(src1, src2)

cv.waitKey(0)
cv.destroyAllWindows()
Пример #7
0
    else:
        img_path = args.image_path
        img = cv2.imread(img_path, cv2.IMREAD_COLOR)
        img_copy = img.copy()
        img_to_cut = img.copy()

    while True:
        if video:
            _, img = cam.read()
            img_to_cut = img.copy()

        cv2.imshow('image', img)
        key = cv2.waitKey(100)

        if key == 115:  # 's'
            cv2.namedWindow('select points')
            cv2.setMouseCallback('select points', get_mouse_points)
            selecting_points = True

        if selecting_points:
            if get_frame and video:
                img_copy = img.copy()
                get_frame = False
            cv2.imshow('select points', img_copy)

        if key == 99 and len(all_points) > 2 and selecting_points:  # 'c'
            selected_points = True
            selecting_points = False
            cv2.setMouseCallback(
                'select points', lambda event, x, y, flags, param: None)
Пример #8
0
        img = cv2.erode(img, None, iterations=2)  #1
        img = cv2.dilate(img, None, iterations=4)  #2
        img = cv2.medianBlur(img, 5)  #3

        img2 = np.copy(img)

        h, w = img.shape
        keypoints = self.detector.detect(img)
        return keypoints, img2


def nothing(x):
    pass


cv2.namedWindow('image3')
cv2.namedWindow('image')
cv2.createTrackbar('tresh_r', 'image3', 0, 255, nothing)
cv2.createTrackbar('tresh_l', 'image3', 0, 255, nothing)
cv2.namedWindow('image2')
el = eye('l')
er = eye('r')


def find_eyes(frame):
    frame = cv2.rectangle(frame, el.ld, el.ur, (0, 255, 0), 1)
    frame = cv2.rectangle(frame, er.ld, er.ur, (0, 255, 0), 1)
    return frame


def do_frame(frame):
Пример #9
0
# start the video stream thread

print('[INFO] starting video stream thread...')
vs = FileVideoStream(args['video']).start()
fileStream = True

vs = VideoStream(src=0).start()

# vs = FileVideoStream(args["video"]).start()

fileStream = False
time.sleep(1.0)

# opening windows to display images and moving them to right positions

cv2.namedWindow('Frame')
cv2.moveWindow('Frame', 0, 0)

cv2.namedWindow('Left Eye')
cv2.moveWindow('Left Eye', 550, 0)
cv2.namedWindow('Equalized Left Eye')
cv2.moveWindow('Equalized Left Eye', 550, 300)

cv2.namedWindow('Right Eye')
cv2.moveWindow('Right Eye', 950, 0)
cv2.namedWindow('Equalized Right Eye')
cv2.moveWindow('Equalized Right Eye', 950, 300)

img_no = 0
phone_call_done = 0
flashlight_done = 0
Пример #10
0
def Q_B(event, x, y, flag, param):
    global score, img, QB
    if event == cv2.EVENT_LBUTTONDOWN:
        if not QB[y][x].any() == 0:
            stp_x = y // img.shape[0]
            stp_y = x // img.shape[1]
            for a in range(1, img.shape[0]):
                for b in range(1, img.shape[1]):
                    QB[stp_x * img.shape[0] + a][stp_y * img.shape[1] +
                                                 b] = (0, 0, 0)
            score += 1


path = os.getcwd()
img = cv2.imread(path + '\\QB.jpg')
cv2.namedWindow('QB')
cv2.setMouseCallback('QB', Q_B)
while (1):
    QB = numpy.zeros((img.shape[0] * 3, img.shape[1] * 3 + 139, img.shape[2]),
                     dtype=numpy.uint8)
    for a in range(0, 3):
        for b in range(0, 3):
            stp = random.randint(0, 1)
            if (stp):
                for c in range(0, img.shape[0]):
                    for d in range(0, img.shape[1]):
                        QB[a * img.shape[0] + c][b * img.shape[1] +
                                                 d] = img[c][d]
            cv2.rectangle(QB, (b * img.shape[1], a * img.shape[0]),
                          ((b + 1) * img.shape[1], (a + 1) * img.shape[0]),
                          (187, 197, 57), 1)
Пример #11
0
"test"

from cv2 import cv2
import numpy as np

img = cv2.imread('Data/00.jpg')

cv2.namedWindow('Window', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Window', 600, 600)

cv2.imshow('Window', img)
#cv2.imwrite('Output/test.png', img)

cv2.waitKey(0)
cv2.destroyAllWindows()
Пример #12
0
def yolo():
    #camera = cv2.VideoCapture(0)
    h, w = None, None
    """
    End of:
    Reading stream video from camera
    """
    """
    Start of:
    Loading YOLO v3 network 
    """

    with open('yolo-coco-data/coco.names') as f:
        labels = [line.strip() for line in f]

    network = cv2.dnn.readNetFromDarknet('yolo-coco-data/yolov3.cfg',
                                         'yolo-coco-data/yolov3.weights')

    layers_names_all = network.getLayerNames()

    layers_names_output = \
    [layers_names_all[i[0] - 1] for i in network.getUnconnectedOutLayers()]

    probability_minimum = 0.5

    threshold = 0.3

    colours = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')
    """
    End of:
    Loading YOLO v3 network
    """
    """
    Start of:
    Reading frames in the loop
    """

    start_time = time.time()
    seconds = 10
    img = cv2.imread('Car.jpg')
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img2 = cv2.imread('Person.png')
    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

    print('Our two images are')
    # plt.imshow(img)
    # plt.show()
    # plt.imshow(img2)
    # plt.show()

    print('Objects are: ')
    all_objects = []
    k = 1
    while (k):
        #_, frame = camera.read()
        frame = img
        if (k == 2):
            frame = img2
        if w is None or h is None:
            h, w = frame.shape[:2]

        current_time = time.time()
        elapsed_time = current_time - start_time

        if elapsed_time > seconds:
            # Releasing camera
            #camera.release()
            # Destroying all opened OpenCV windows
            cv2.destroyAllWindows()

            break
        """
        Start of:
            Getting blob from current frame
            """
        blob = cv2.dnn.blobFromImage(frame,
                                     1 / 255.0, (416, 416),
                                     swapRB=True,
                                     crop=False)
        """
        End of:
        Getting blob from current frame
        """
        """
        Start of:
        Implementing Forward pass
        """

        network.setInput(blob)  # setting blob as input to the network
        #start = time.time()
        output_from_network = network.forward(layers_names_output)
        #end = time.time()
        #print('Current frame took {:.5f} seconds'.format(end - start))
        """
        End of:
            Implementing Forward pass
        """
        """
        Start of:
        Getting bounding boxes
        """

        bounding_boxes = []
        confidences = []
        class_numbers = []
        for result in output_from_network:
            # Going through all detections from current output layer
            for detected_objects in result:
                # Getting 80 classes' probabilities for current detected object
                scores = detected_objects[5:]
                # Getting index of the class with the maximum value of probability
                class_current = np.argmax(scores)
                # Getting value of probability for defined class
                confidence_current = scores[class_current]

                if confidence_current > probability_minimum:
                    box_current = detected_objects[0:4] * np.array(
                        [w, h, w, h])
                    x_center, y_center, box_width, box_height = box_current
                    x_min = int(x_center - (box_width / 2))
                    y_min = int(y_center - (box_height / 2))

                    bounding_boxes.append(
                        [x_min, y_min,
                         int(box_width),
                         int(box_height)])
                    confidences.append(float(confidence_current))
                    class_numbers.append(class_current)

        if (k == 2):
            k = 0
        if (k == 1):
            k = 2
        """
        End of:
        Getting bounding boxes
        """
        """
        Start of:
        Non-maximum suppression
        """
        results = cv2.dnn.NMSBoxes(bounding_boxes, confidences,
                                   probability_minimum, threshold)
        """
        End of:
        Non-maximum suppression
        """
        """
        Start of:
        Drawing bounding boxes and labels
        """

        if len(results) > 0:
            # Going through indexes of results
            for i in results.flatten():
                #speech output
                obj = labels[int(class_numbers[i])]
                #if obj in all_objects:
                #    continue
                #else:
                all_objects.append(obj)
                #text = "There is a "+obj+" in front of you."
                #language = 'en'
                #speech = gTTS(text = text, lang = language, slow = False)
                #speech.save("text.wav")
                #os.system("text.wav")

                x_min, y_min = bounding_boxes[i][0], bounding_boxes[i][1]
                box_width, box_height = bounding_boxes[i][2], bounding_boxes[
                    i][3]

                colour_box_current = colours[class_numbers[i]].tolist()

                #cv2.rectangle(frame, (x_min, y_min),
                #          (x_min + box_width, y_min + box_height),
                #          colour_box_current, 2)
                # plt.imshow(cv2.rectangle(frame, (x_min, y_min),
                #           (x_min + box_width, y_min + box_height),
                #           colour_box_current, 2))
                # plt.show()

                arr = cv2.rectangle(frame, (x_min, y_min),
                                    (x_min + box_width, y_min + box_height),
                                    colour_box_current, 2)
                img = Image.fromarray(arr.astype('uint8'))

                # create file-object in memory
                file_object = io.BytesIO()

                # write PNG in file-object
                img.save(file_object, 'PNG')

                # move to beginning of file so `send_file()` it will read from start
                file_object.seek(0)

                return send_file(file_object, mimetype='image/PNG')

                print(obj)
                print(x_min)
                print(y_min)
                print(box_width)
                print(box_height)
                text_box_current = '{}: {:.4f}'.format(
                    labels[int(class_numbers[i])], confidences[i])

                cv2.putText(frame, text_box_current, (x_min, y_min - 5),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, colour_box_current,
                            2)

        #print(len(all_objects))
        """
        End of:
        Drawing bounding boxes and labels
        """
        """
        Start of:
        Showing processed frames in OpenCV Window
        """
        cv2.namedWindow('YOLO v3 Real Time Detections', cv2.WINDOW_NORMAL)
        # Pay attention! 'cv2.imshow' takes images in BGR format
        cv2.imshow('YOLO v3 Real Time Detections', frame)

        # Breaking the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        """
        End of:
        Showing processed frames in OpenCV Window
        """
    """
    End of:
    Reading frames in the loop
    """

    for i in all_objects:
        print(i)
    # Releasing camera
    #camera.release()
    # Destroying all opened OpenCV windows
    cv2.destroyAllWindows()

    return send_file(file_object, mimetype='image/PNG')
Пример #13
0
def getVideo_Info(modelpath, video_path, output_path=""):
    from cv2 import cv2
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        raise IOError("Couldn't open webcam or video")
    video_FourCC = int(cap.get(cv2.CAP_PROP_FOURCC))  # 获取原始视频的信息
    video_fps = cap.get(cv2.CAP_PROP_FPS)
    video_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
                  int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    isOutput = True if output_path != "" else False  # 如果设置了视频保存路径,则保存视频
    if isOutput:
        print("!!! TYPE:", type(output_path), type(video_FourCC),
              type(video_fps), type(video_size))
        out = cv2.VideoWriter(output_path, video_FourCC, video_fps,
                              video_size)  # 根据原视频设置 保存视频的路径、大小、帧数
    accum_time = 0
    curr_fps = 0
    fps = "FPS: ??"
    prev_time = time.time()
    yolo = YOLO()
    pose_model = general_coco_model(modelpath)  # 1.加载模型
    while True:
        return_value, frame = cap.read()

        if return_value:
            # 骨骼
            bone_points = pose_model.getBoneKeypoints(frame)  # 2.骨骼关键点
            lineimage, dotimage = pose_model.vis_bone_pose(
                frame, bone_points)  # 骨骼连线图、标记图显示
            # list1 = getBoneInformation(bone_points)  # 3.骨骼特征

            temp, labelinfo = yolo.detect_image(
                Image.fromarray(frame), Image.fromarray(lineimage))  # 检测PIL格式
            result = np.asarray(temp)  # 画图到全部图上
            curr_time = time.time()
            exec_time = curr_time - prev_time
            prev_time = curr_time
            accum_time = accum_time + exec_time
            curr_fps = curr_fps + 1
            if accum_time > 1:
                accum_time = accum_time - 1
                fps = "FPS: " + str(curr_fps)
                curr_fps = 0
            cv2.putText(result,
                        text=fps,
                        org=(3, 15),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=0.50,
                        color=(255, 0, 0),
                        thickness=2)
            cv2.putText(result,
                        "q-'quit'",
                        org=(3, 45),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=0.50,
                        color=(0, 255, 0),
                        thickness=2)  # 标注字体
            cv2.namedWindow("result", cv2.WINDOW_NORMAL)
            cv2.imshow("result", result)
            if isOutput:
                out.write(result)
        else:
            print("Frame is end!")
            break
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Пример #14
0
def tracker(video_path,
            background_full,
            rois,
            threshold=5,
            display=True,
            area_min=0,
            area_max=1000,
            split_range=False):
    """ Function that takes a video path, a background file, rois, threshold and display switch. This then uses
    background subtraction and centroid tracking to find the XZ coordinates of the largest contour. Saves out a csv file
     with frame #, X, Y, contour area"""

    print("tracking {}".format(video_path))

    # As camera is often excluded, check here and buffer if not included
    if len(rois) == 1:
        rois['cam'] = 'unknown'

    # load video
    video = cv2.VideoCapture(video_path)

    if display:
        # create display window
        cv2.namedWindow("Live thresholded")
        cv2.namedWindow("Live")

    # as there can be multiple rois the writer, data and moviename are kept in lists
    data = list()
    frame_id = 0
    for roi in np.arange(0, len(rois) - 1):
        data.append(list())

    if split_range is False:
        total = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
        split_range = [0, total + 1]
        split_name = False
    else:
        split_name = True

    while video.isOpened():
        ret, frame = video.read()
        if not ret:
            print("reached end of video")
            video.release()
            break
        if frame_id in np.arange(split_range[0], split_range[1]):
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            frameDelta_full = cv2.absdiff(background_full, gray)

            # tracking
            cx = list()
            cy = list()
            contourOI = list()
            contourOI_ = list()
            for roi in range(0, len(rois) - 1):
                # for the frame define an ROI and crop image
                curr_roi = rois["roi_" + str(roi)]
                frameDelta = frameDelta_full[curr_roi[1]:curr_roi[1] +
                                             curr_roi[3],
                                             curr_roi[0]:curr_roi[0] +
                                             curr_roi[2]]
                image_thresholded = cv2.threshold(frameDelta, threshold, 255,
                                                  cv2.THRESH_TOZERO)[1]
                (contours, _) = cv2.findContours(image_thresholded,
                                                 cv2.RETR_EXTERNAL,
                                                 cv2.CHAIN_APPROX_SIMPLE)
                if len(contours) > 0:
                    contourOI_.append(max(contours, key=cv2.contourArea))
                    area = cv2.contourArea(contourOI_[roi])
                    if (area > area_min) and (area < area_max):
                        contourOI.append(cv2.convexHull(contourOI_[roi]))
                        M = cv2.moments(contourOI[roi])
                        cx.append(int(M["m10"] / M["m00"]))
                        cy.append(int(M["m01"] / M["m00"]))
                        data[roi].append((frame_id, cx[roi], cy[roi], area))
                    else:
                        print(
                            "no large enough contour found for roi {}!".format(
                                roi))
                        data[roi].append((frame_id, np.nan, np.nan, np.nan))
                        contourOI_[-1] = False
                        contourOI.append(False)
                        cx.append(np.nan)
                        cy.append(np.nan)
                else:
                    print("no contour found for roi {}!".format(roi))
                    data[roi].append((frame_id, np.nan, np.nan, np.nan))
                    contourOI_.append(False)
                    contourOI.append(False)
                    cx.append(np.nan)
                    cy.append(np.nan)

            if frame_id % 500 == 0:
                print("Frame {}".format(frame_id))
            if display:
                full_image_thresholded = (cv2.threshold(
                    frameDelta_full, threshold, 255, cv2.THRESH_TOZERO)[1])
                # Live display of full resolution and ROIs
                cv2.putText(full_image_thresholded,
                            "Framenum: {}".format(frame_id),
                            (30, full_image_thresholded.shape[0] - 30),
                            cv2.FONT_HERSHEY_SIMPLEX,
                            fontScale=0.5,
                            color=255)

                for roi in range(0, len(rois) - 1):
                    if np.all(contourOI_[roi] != False):
                        curr_roi = rois["roi_" + str(roi)]
                        # add in contours
                        corrected_contour = np.empty(contourOI_[roi].shape)
                        corrected_contour[:, 0,
                                          0] = contourOI_[roi][:, 0,
                                                               0] + curr_roi[0]
                        corrected_contour[:, 0,
                                          1] = contourOI_[roi][:, 0,
                                                               1] + curr_roi[1]
                        cv2.drawContours(full_image_thresholded,
                                         corrected_contour.astype(int), -1,
                                         255, 1)

                        # add in centroid
                        cv2.circle(
                            full_image_thresholded,
                            (cx[roi] + curr_roi[0], cy[roi] + curr_roi[1]), 8,
                            255, 1)

                cv2.imshow("Live thresholded", full_image_thresholded)
                cv2.imshow("Live", gray)
                cv2.waitKey(1)

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

        frame_id += 1

    # saving data
    print("Saving data output")
    date = datetime.datetime.now().strftime("%Y%m%d")

    for roi in range(0, len(rois) - 1):
        datanp = np.array(data[roi])
        if split_name is False:
            filename = video_path[
                0:-4] + "_tracks_{}_Thresh_{}_Area_{}_roi-{}.csv".format(
                    date, threshold, area_min, roi)
        else:
            range_s = str(split_range[0]).zfill(5)
            range_e = str(split_range[1]).zfill(5)
            filename = video_path[
                0:-4] + "_tracks_{}_Thresh_{}_Area_{}_Range{}-{}_.csv".format(
                    date, threshold, area_min, range_s, range_e)
        os.makedirs(os.path.dirname(filename), exist_ok=True)
        np.savetxt(filename, datanp, delimiter=",")

    print("Tracking finished on video cleaning up")
    cv2.destroyAllWindows()
Пример #15
0
    input_image = os.path.join('', "", filename_input)
    input_mask = os.path.join('', "", filename_mask)
    output_image = os.path.join('', "", filename_output)

    if filename_input is None:
        print('File input not exist.')
        exit()
    img = cv2.imread(filename_input)
    mask = np.zeros(img.shape, np.uint8)
    # handle_remove(input_image, output_image, input_mask,
    #               mask, img)

    img_copy = img.copy()
    mask_copy = mask.copy()
    window_name = 'Draw mask. s:save; r: reset; q:quit'
    cv2.namedWindow(window_name)
    cv2.setMouseCallback(window_name, draw_circle)
    while True:
        cv2.imshow(window_name, img)
        k = cv2.waitKey(1) & 0xFF
        # cv2.imshow('mask', mask)
        if k == ord('r'):
            img = img_copy.copy()
            mask = mask_copy.copy()
        elif k == ord("s"):
            cv2.imwrite('mask.png', mask)
            print('[INFO]:    processing')
            object_removal(input_image, output_image, input_mask)
            break
        elif k == ord("q"):
            cv2.destroyAllWindows()
Пример #16
0
    if event == cv2.EVENT_LBUTTONDOWN:
        REF_POINT = [(x, y)]
        print('mouse down', x, y)
    elif event == cv2.EVENT_LBUTTONUP:
        # record the ending (x, y) coordinates and indicate that
        # the cropping operation is finished
        REF_POINT.append((x, y))
        RESULT_DICT[FIELD].append(REF_POINT)
        print('mouse up', x, y)
        # draw a rectangle around the region of interest
        cv2.rectangle(IMAGE, REF_POINT[0], REF_POINT[1], (0, 255, 0), 2)
        cv2.imshow("image", IMAGE)
        print(RESULT_DICT)


cv2.namedWindow("image")
cv2.setMouseCallback("image", getBBs)

############################################################################################################
# Sort Pics
############################################################################################################

BREED_COUNT = 0
IMAGE_COUNT = 0
BREED = BREEDS[BREED_COUNT]
print('BREED:', BREED)
print(len(PICS), '# of pics')
PICS = pAnimals[pAnimals['breed'] == BREED].path.tolist()


def loadIm(breed, randPic=False):
Пример #17
0
from cv2 import cv2
import numpy as np

def read_cam(camId):
    frameWidth = 440
    frameHeight = 380
    cap = cv2.VideoCapture(camId)
    cap.set(3, frameWidth)
    cap.set(4, frameHeight)
    cap.set(10,150)
    return cap

def empty(a):
    pass

cv2.namedWindow("HSV")
cv2.resizeWindow("HSV",640,240)
cv2.createTrackbar("HUE Min","HSV",0,179,empty)
cv2.createTrackbar("SAT Min","HSV",0,255,empty)
cv2.createTrackbar("VALUE Min","HSV",0,255,empty)
cv2.createTrackbar("HUE Max","HSV",179,179,empty)
cv2.createTrackbar("SAT Max","HSV",255,255,empty)
cv2.createTrackbar("VALUE Max","HSV",255,255,empty)



cap = read_cam(0)

while True:
    _, img = cap.read()
    imgHsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
Пример #18
0
    def _execute(self, payload):
        frame = payload.original_frame
        raw_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        frames = self.frames
        median = np.median(
            frames, axis=0).astype(float) if len(frames) else np.ndarray(
                (0, ))  # .reshape(100, 100)

        if len(self.detection_mask) == 0:
            self.detection_mask = np.zeros(raw_gray.shape, dtype='uint8')

        # df = payload.dfs.get('detections', pd.DataFrame()).drop_duplicates()
        # if len(df) > 0:

        vehicle_detections = list(payload.vehicle_detections)

        boxes = (d.bounding_box for d in vehicle_detections)
        # boxes = (b.get_scaled(0.5) for b in boxes)
        bb_h_percentage = 0.2
        boxes = [
            BoundingBox(b.x, int(round(b.y + b.h * (1 - bb_h_percentage))),
                        b.w, int(round(b.h * bb_h_percentage))) for b in boxes
        ]
        for box in boxes:
            # y_start = max(0, box.y)
            # y_end = max(box.y + box.h, 0)
            # x_start = max(0, box.x)
            # x_end = max(0, box.x + box.w)
            # self.detection_mask[y_start: y_end, x_start:x_end] = 1
            self.detection_mask[box.y:box.y + box.h, box.x:box.x + box.w] = 1

        # cv2.imshow('detection_mask',self.detection_mask*255)
        # cv2.waitKey(0)

        gray = raw_gray.copy()  # * (1 - self.detection_mask)
        if len(median):
            idxs = np.where(self.detection_mask == 1)
            detections_median = np.median(median[idxs])
            gray[idxs] = detections_median

        self.frames.append(gray)

        title = 'Median'
        cv2.namedWindow(title, cv2.WINDOW_NORMAL)
        if len(median):
            cv2.imshow(title, median / 255)
        # cv2.imshow(title, frame)
        # cv2.imshow(title, gray / 255)

        # cv2.imwrite(r'median.jpg', median, )

        return payload
        src = median / 255

        from experimental import demo_erosion_dilatation
        src = (1 - src) * 255
        demo_erosion_dilatation(src, iterations=2)

        erosion_size = 5
        erosion_type = cv2.MORPH_ELLIPSE
        element = cv2.getStructuringElement(
            erosion_type, (2 * erosion_size + 1, 2 * erosion_size + 1),
            (erosion_size, erosion_size))
        erosion_dst = cv2.erode(src, element, iterations=1)
        cv2.imshow('erosion', erosion_dst)

        erosion_dst = cv2.dilate(src, element, iterations=2)
        cv2.imshow('dialation', erosion_dst)

        cv2.waitKey(0)

        element = cv2.getStructuringElement(
            erosion_type, (2 * erosion_size + 1, 2 * erosion_size + 1),
            (erosion_size, erosion_size))

        cv2.waitKey(1)
        # ============================================

        return payload
Пример #19
0
    gt_bbox = get_gt_box(gt_box_file)
    for i in range(gt_bbox.shape[0]):
        label[gt_bbox[i, 2]:gt_bbox[i, 4], gt_bbox[i, 1]:gt_bbox[i, 3], 0] = gt_bbox[i, 0]
    return label

def get_image(image_file):
    image = np.array(Image.open(image_file))
    height, width, channels = image.shape
    image = image.astype((np.int32))
    return image, height, width

main_box_dir = 'E:/02竞赛/水下目标检测/water_optical_comp/train/train/box/'
main_image_dir = 'E:/02竞赛/水下目标检测/water_optical_comp/train/train/image/'

for i in range(1,1000):
    image_file = main_image_dir + str("%06d" % i) + '.jpg'
    gt_box_file = main_box_dir + str("%06d" %  i) + '.xml'
    image, height, width = get_image(image_file)
    label = get_label(gt_box_file, height, width) * 60
    label = np.concatenate((label, label,label),axis=-1).astype(np.uint8)
    image = image.astype(np.uint8)
    cv2.namedWindow("image", 0)
    cv2.resizeWindow("image", 640, 480)
    cv2.imshow("image", image)
    cv2.namedWindow("label", 0)
    cv2.resizeWindow("label", 640, 480)
    cv2.imshow('label' ,label)
    cv2.waitKey(0)


Пример #20
0
# coding: utf-8
import numpy as np
from cv2 import cv2

print(cv2.useOptimized())

img_1 = cv2.imread(r'pictures\lena.jpg')

e1 = cv2.getTickCount()
for i in range(7, 24, 2):
    img_1 = cv2.medianBlur(img_1, i)
e2 = cv2.getTickCount()

cv2.namedWindow("medianBlur")
cv2.imshow('medianBlur', img_1)
cv2.waitKey(0)
cv2.destroyAllWindows()

t = (e2 - e1)/cv2.getTickFrequency()
print('Process time: ',t, 's', sep='')

# Python scalar operations are faster than Numpy scalar operations. So for operations including one or two elements, Python scalar is better than Numpy arrays. Numpy takes advantage when size of array is a little bit bigger.
# Normally, OpenCV functions are faster than Numpy functions. So for same operation, OpenCV functions are preferred. But, there can be exceptions, especially when Numpy works with views instead of copies.

# Avoid using loops in Python as far as possible, especially double/triple loops etc. They are inherently slow.
# Vectorize the algorithm/code to the maximum possible extent because Numpy and OpenCV are optimized for vector operations.
# Exploit the cache coherence.
# Never make copies of array unless it is needed. Try to use views instead. Array copying is a costly operation.
# Even after doing all these operations, if your code is still slow, or use of large loops are inevitable, use additional libraries like Cython to make it faster.

# More performance optimization will learn in the future.
from cv2 import cv2
import numpy as np


def nothing(x):
    pass


vid = cv2.VideoCapture(0)

cv2.namedWindow("Tracking")
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing)

cv2.createTrackbar("UH", "Tracking", 255, 255, nothing)
cv2.createTrackbar("US", "Tracking", 255, 255, nothing)
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing)

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

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lh = cv2.getTrackbarPos("LH", "Tracking")
    ls = cv2.getTrackbarPos("LS", "Tracking")
    lv = cv2.getTrackbarPos("LV", "Tracking")

    uh = cv2.getTrackbarPos("UH", "Tracking")
    us = cv2.getTrackbarPos("US", "Tracking")
    uv = cv2.getTrackbarPos("UV", "Tracking")
def draw_function(event, x, y, flags, param):
    if event == cv2.EVENT_MOUSEMOVE:
        global b, g, r, xpos, ypos, moved
        moved = True
        xpos = x
        ypos = y
        b, g, r = img[
            y,
            x]  #BGR, since OpenCV represents images as NumPy arrays in reverse order
        b = int(b)
        g = int(g)
        r = int(r)


cv2.namedWindow("colors_win")
cv2.setMouseCallback("colors_win", draw_function)

while (1):
    cv2.imshow("colors_win", img)
    if (moved):

        #text display config
        cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1)

        text = getName(r, g,
                       b) + " R=" + str(r) + " G=" + str(g) + " B=" + str(b)

        cv2.putText(img, text, (50, 50), 2, 0.8, (255, 255, 255), 2,
                    cv2.LINE_AA)
Пример #23
0
import pyautogui
from cv2 import cv2
import numpy as np


resolution = (1366, 768) #Screen Resolution

codec = cv2.VideoWriter_fourcc(*"XVID") #codex



out = cv2.VideoWriter("Recorded.avi", codec, 60, resolution)

cv2.namedWindow("Recording", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Recording", 480, 270)

while True:
    img = pyautogui.screenshot() #capturing screenshot
    frame = np.array(img) #converting the image into numpy array representation 
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #converting the BGR image into RGB image
    out.write(frame) #writing the RBG image to file
    cv2.imshow('Recording', frame) #display screen/frame being recorded
    if cv2.waitKey(1) == ord('q'): #Wait for the user to press 'q' key to stop the recording
        break

out.release()
cv2.destroyAllWindows()



Пример #24
0
def main(stream, photo):
    with tf.compat.v1.Session() as sess:
        # Get YOLOV3 model from tensornet and store it.
        inputs = tf.compat.v1.placeholder(tf.float32, [None, 416, 416, 3]) 
        model = nets.YOLOv3COCO(inputs, nets.Darknet19)
        # Choose the class to identify. I'm only need the person class to count people.
        classes = {'0' : 'person'}
        indexOfClasses = [0]
        # Train the model.
        sess.run(model.pretrained())

        # Launch webcam to capture the image from the camera. 0 is for the webcam.
        webcamStream = cv.VideoCapture(photo)
        # Start main loop for the webcam stream.
        while(webcamStream.isOpened()):
            # Get the image of the stream.
            ret, frame = webcamStream.read()

            # Create new image based on the stream shape.
            img = cv.resize(frame, (416, 416))
            imgTmp = np.array(img)
            imgTmp = np.reshape(imgTmp, (-1, 416, 416, 3))

            # Run model with the image of the stream for classification and identifying people.
            preds = sess.run(model.preds, {inputs: model.preprocess(imgTmp)})

            # Create boxes for people in the image.
            boxes = model.get_boxes(preds, imgTmp.shape[1:3])

            # Create a windows to display the image.
            cv.namedWindow('image', cv.WINDOW_NORMAL)
            cv.resizeWindow('image', 500, 500)

            # Loop to create the boxes for people found in image if in it and draw the boxe around.
            # Also display the number of classes identify. If 2 people on camera should display 'Number of person: 2'.
            boxes1 = np.array(boxes)
            for classe in indexOfClasses:
                count = 0
                label = classes[str(classe)]
                if len(boxes1) != 0:
                    for index in range(len(boxes1[classe])): 
                        box = boxes1[classe][index]
                        if boxes1[classe][index][4] >= .40: 
                            count += 1
                            cv.rectangle(img, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 3)
                            cv.putText(img, label, (box[0], box[1]), cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), lineType=cv.LINE_AA)
                print('Number of person: ', count)

            # Display the image.
            cv.imshow("image", img)

            # If you give an image as input, show the image with the result for 5 secondes and stop the program.
            if photo != 0:
                cv.waitKey(5000)
                webcamStream.release()
                cv.destroyAllWindows()
                break

            # If you're in a real-time stream and you press 'q', this will quit stop the program.
            if stream == True:
                if cv.waitKey(1) & 0xFF == ord('q'):
                    webcamStream.release()
                    cv.destroyAllWindows()
                    break
Пример #25
0
#reads folder with pictures as processed frames
#user creates set of images
import argparse
import os
from cv2 import cv2
import numpy as np

#parser to point wich video we want to process in floder
parser = argparse.ArgumentParser(description='VID number.')
parser.add_argument('number', metavar='N', type=int, nargs=1, help='number of video to process')
parser.add_argument('frameN', metavar='F', type=int, nargs=1, help='video start frame number starting from 1')
args = parser.parse_args()
path='VID/15FPS/to_do' #main folder with videos
save_path = '/Pictures/cd'
path_to_folder=path + '/' + str(args.number[0])
cv2.namedWindow("old", cv2.WINDOW_NORMAL)
cv2.namedWindow("new", cv2.WINDOW_NORMAL)
class Found(Exception): pass
licznikY = 1684
licznikN = 2855
licznikSN = 1
con = 0

def numbers(img):
    image = img.copy()
    _, width = image.shape[:2]
    n = int(width/224) #number of images
    pixels = 50 #where to draw number
    for i in range(n):
        cv2.putText(image, str(i), (pixels, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        pixels += 224
Пример #26
0
            # Check if drawing boundary boxes in span of images that has been selected.
            if bounding_box.bounding_box_start_coordinates_x_y[2] in range(create_text_file.cell_numbers_list_for_each_grid[-2], \
                 create_text_file.cell_numbers_list_for_each_grid[-1]+1)  \
            or bounding_box.bounding_box_start_coordinates_x_y[2] in range(create_text_file.cell_numbers_list_for_each_grid[-1], \
                 create_text_file.cell_numbers_list_for_each_grid[-2]+1):
                draw_boundary_box(x, y, bounding_box.bounding_box_start_coordinates_x_y)


# Check if camera opened successfully.
if not cap.isOpened():
    print("Error opening video stream or file")
    sys.exit()

index = 0

cv2.namedWindow('image_selector_from_video', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image_selector_from_video', grid.window_width, grid.window_height)


# Read until video is completed
def image_grid(index, x_offset=0, y_offset=0, i=0):
    cap.set(1, index)
    recalculate_window_stuff()
    # Resetting large image/grid to black each time.
    l_img = np.zeros((grid.window_height, grid.window_width, 3), np.uint8)
    cv2.imshow('image_selector_from_video', l_img)
    cv2.waitKey(1)
    index_for_frame_list = index

    while i < grid.number_of_cells:
Пример #27
0
if __name__ == '__main__':

    def get_color(x: int, y: int, img: np.ndarray) -> (int, int, int):
        """ retrieve the b r g color values from the given image """
        b, g, r, *_ = map(int, (*img[y, x], ))  # change to int
        return b, g, r

    def on_click(event, x, y, flags, params):
        """ when the image is clicked, set the hsv trackbars """
        if event == cv.EVENT_LBUTTONUP:
            b, g, r = get_color(x, y, params)
            hsv.set_trackbars(b, g, r)

    # make a resizable window
    window_name = 'Image'
    cv.namedWindow(window_name, cv.WINDOW_GUI_NORMAL)

    image_file = "/some/path/to/image.png"  # <- path to local image here

    # create the window with the trackbars
    hsv = HSVTrackbars()
    hsv.tolerance = 5

    while bool(cv.getWindowProperty(window_name, cv.WND_PROP_VISIBLE)):
        try:
            img = cv.imread(image_file)
            img = hsv.apply_hsv(img)

            # show the image
            cv.imshow(window_name, img)
            # click on the image to set the hsv values
leftCam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
rightCam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
leftCam.set(cv2.CAP_PROP_AUTO_WB, 0)
rightCam.set(cv2.CAP_PROP_AUTO_WB, 0)


def none(x):
    pass


leftBrightness = "LeftBrightness"
rightBrightness = "RightBrightness"
leftExpo = "LeftExpo"
rightExpo = "RightExpo"
trackbarName = "Console"
cv2.namedWindow(trackbarName)
cv2.createTrackbar(leftBrightness, trackbarName, 0, 255, none)
cv2.createTrackbar(rightBrightness, trackbarName, 0, 255, none)
cv2.createTrackbar(leftExpo, trackbarName, 0, 5, none)
cv2.createTrackbar(rightExpo, trackbarName, 0, 5, none)

leftCam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
rightCam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)

leftCam.set(cv2.CAP_PROP_FPS, 30)
rightCam.set(cv2.CAP_PROP_FPS, 30)

while True:
    _, leftframe = leftCam.read()
    _, rightframe = rightCam.read()
Пример #29
0
    scipy.io.savemat(fileName + str(time) + '.mat', {'csvmatrix': matrix})


##### read fileName and tagSize#####

tagSize = float(input("Enter the tag Size: "))
fileName = input("Enter the file Name")
xTag = float(input("Input the x-coordinate in cm: "))
yTag = float(input("Input the y-coordinate in cm: "))
zTag = float(input("Input the z-coordinate in cm: "))

######## Location of tag ##############
tagLoc = np.array([-xTag / 100, yTag / 100, zTag / 100, 1])

# import rectangleArea as ra
cv2.namedWindow("preview")
cap = cv2.VideoCapture(0)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

###################  Hard Coded calibration matrix for the calibration images in the image folder #########
mtx = np.array([[1.16853450e+03, 0.00000000e+00, 9.45814963e+02],
                [0.00000000e+00, 1.15460499e+03, 5.37990908e+02],
                [0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])

dist = np.array([[
    2.49754342e-01, -6.08282123e-01, -3.96471621e-04, 4.01400822e-03,
    3.04199503e-01
]])
Пример #30
0
import tensorflow as tf
import numpy as np
import os
from cv2 import cv2
from PIL import Image
import config as cfg
import ImageProcessLib as imgLib

# ppt = imgLib.PPT()
cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
cv2.namedWindow("segment")

with tf.Session() as sess:
    """读取模型"""
    saver = tf.train.import_meta_graph('./My_Model/FCN_Model.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./My_Model'))
    graph = tf.get_default_graph()
    # for tensor_name in tf.contrib.graph_editor.get_tensors(tf.get_default_graph()):
    #     print(tensor_name)
    img_PH = graph.get_tensor_by_name("img_PH:0")
    batch_PH = graph.get_tensor_by_name("batch_PH:0")
    drop_PH = graph.get_tensor_by_name("drop_PH:0")
    output = graph.get_tensor_by_name("output:0")
    """识别和定位"""
    while (1):
        ret, srcIMG = cap.read()
        rgbIMG = cv2.cvtColor(srcIMG, cv2.COLOR_BGR2RGB)
        smallIMG = cv2.resize(rgbIMG,
                              (cfg.IMG_W, cfg.IMG_H))  # 网络输入图像大小224*160
        image_4 = smallIMG[np.newaxis, :, :]  # 扩展维度
        image_n = imgLib.image_normalize(image_4)  # 标准化