Programming/openCV2024. 1. 16. 11:34

MTM + webcam

pc에서는 잘도는데 arm에서 잘되려나..

 

import MTM, cv2
import numpy as np

letter_a = cv2.imread('letter_a.png', 0)
letter_b = cv2.imread('letter_b.png', 0)
letter_c = cv2.imread('letter_c.png', 0)
letter_d = cv2.imread('letter_d.png', 0)

letter_a.astype(np.uint8)
letter_b.astype(np.uint8)
letter_c.astype(np.uint8)
letter_d.astype(np.uint8)

listTemplates = [('A', letter_a),
                 ('B', letter_b),
                 ('C', letter_c),
                 ('D', letter_d)]

webcam = cv2.VideoCapture(2)
webcam.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)
webcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)

def drawBoxesOnRGB2(image, tableHit, boxThickness=2, boxColor=(255, 255, 00), showLabel=False, labelColor=(255, 255, 0), labelScale=0.5 ):
    # Convert Grayscale to RGB to be able to see the color bboxes
    if image.ndim == 2: outImage = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) # convert to RGB to be able to show detections as color box on grayscale image
    else:               outImage = image.copy()

    for _, row in tableHit.iterrows():
        x,y,w,h = row['BBox']
        score = row['Score']
        cv2.rectangle(outImage, (x, y), (x+w, y+h), color=boxColor, thickness=boxThickness)
        if showLabel: cv2.putText(outImage, text=row['TemplateName'] + "@" + str(score * 100), org=(x, y), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=labelScale, color=labelColor, lineType=cv2.LINE_AA)

    return outImage

if not webcam.isOpened():
    print("Could not open webcam")
    exit()

while webcam.isOpened():
    status, image = webcam.read()
    image.astype(np.uint8)
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    tableHit = MTM.matchTemplates(listTemplates, image_gray, score_threshold=0.8, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0)
    print("Found {} letters".format(len(tableHit)))
    print(tableHit)

    Overlay = drawBoxesOnRGB2(image, tableHit, showLabel=True)

    if status:
        cv2.imshow("test", Overlay)

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

webcam.release()
cv2.destroyAllWindows()

[링크 : https://medium.com/quantrium-tech/object-detection-multi-template-matching-2c9c9fc1a867]

[링크 : https://github.com/multi-template-matching/MultiTemplateMatching-Python]

 

열고 해상도 바꾸는게 안되면, 열면서 해상도 설정하면 됨.

cap = cv2.VideoCapture(1, apiPreference=cv2.CAP_ANY, params=[
    cv2.CAP_PROP_FRAME_WIDTH, 1280,
    cv2.CAP_PROP_FRAME_HEIGHT, 1024])

[링크 : https://stackoverflow.com/questions/71310212/python-cv2-videocapture-has-wrong-resolution-and-read-cropped-images]

'Programming > openCV' 카테고리의 다른 글

opencv 카메라 캡쳐 - 최신 이미지 갱신  (0) 2024.01.25
opencv webcam 수동촛점 조절  (0) 2024.01.25
opencv cv2.imshow() error  (0) 2024.01.16
opencv를 이용한 다중 템플릿 추적  (0) 2024.01.15
cv2.imshow cv2.waitKey  (0) 2022.03.14
Posted by 구차니