Расстояние guish между распознанными объектами - PullRequest
0 голосов
/ 08 марта 2020

Я пытаюсь решить следующую проблему, и я хотел бы получить помощь.

Код выполняет следующее. Распознает несколько объектов по цвету и сохраняет их в указанной папке. Проблема в том, что теперь все изображения сохраняются в одной папке. Но я хочу, чтобы изображения разных объектов помещались в разные папки. Я хотел бы различать их в зависимости от их размещения в видео.

Мой вопрос, как я могу различить guish их.

Ввод

Выходы

import cv2 as cv
import numpy as np
import os
import uuid
import datetime

cap = cv.VideoCapture(1)
font = cv.FONT_HERSHEY_COMPLEX
path = os.getcwd()
path = path + "/data/"
print(path)


def createFolder(directory):
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
        print('Error: Creating directory. ' + directory)


createFolder("./data")

# folderName = '%s' % (str(uuid.uuid4()))
# start = datetime.datetime.now()
while cap.isOpened():

    _, frame = cap.read()
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    # blue is the chosen one now
    lower_color = np.array([82, 33, 39])
    upper_color = np.array([135, 206, 194])

    mask = cv.inRange(hsv, lower_color, upper_color)
    kernel = np.ones((5, 5), np.uint8)
    mask = cv.erode(mask, kernel)

    contours, hierarchy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

    # find contour
    for contour in contours:
        area = cv.contourArea(contour)
        # x, y, h, w = cv.boundingRect(contour)

        if area > 100:
            # bounding box
            # cv.rectangle(frame, (x - 40, y - 30), (x + h * 3, y + w * 3), (0, 255, 0), 1)
            # cutting and saving
            ext_left = tuple(contour[contour[:, :, 0].argmin()][0] - 20)
            ext_right = tuple(contour[contour[:, :, 0].argmax()][0] + 20)
            ext_top = tuple(contour[contour[:, :, 1].argmin()][0] - 20)
            ext_bot = tuple(contour[contour[:, :, 1].argmax()][0] + 20)
            cropped_image = frame[ext_top[1]:ext_bot[1], ext_left[0]:ext_right[0]]
            # cv.imwrite(outfile, cropped_image)

            # generate name for each picture
            outfile = '%s.jpg' % (str(uuid.uuid4()))
            # write images to a specified folder
            cv.imwrite(os.path.join(path, outfile), cropped_image)

            # print contour boxes coordinates
            print(ext_left, ext_right, ext_top, ext_top)

    # outputs
    cv.imshow("Frame", frame)
    cv.imshow("Mask", mask)

    key = cv.waitKey(1)
    if key == 27:
        break
#end = datetime.datetime.now()
#elapsed = end - start
#print(elapsed.seconds, ":", elapsed.microseconds)

cap.release()
cv.destroyAllWindows()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...