본문 바로가기

OpenCV

ch13 QRCode

#python

import cv2

def decode_qrcode():
    cap = cv2.VideoCapture(0)

    if not cap.isOpened():
        print("Camera open failed!")
        return

    detector = cv2.QRCodeDetector()

    while True:
        ret, frame = cap.read()

        if not ret:
            print("Frame load failed!")
            break

        info, points, _ = detector.detectAndDecode(frame)

        if info:
            points = points.reshape((-1, 1, 2))
            cv2.polylines(frame, [points], True, (0, 0, 255), 2)
            cv2.putText(frame, info, (10, 30), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255))

        cv2.imshow("frame", frame)
        if cv2.waitKey(1) == 27:
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    decode_qrcode()

// C++

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

void decode_qrcode();

int main(void)
{
	decode_qrcode();

	return 0;
}

void decode_qrcode()
{
	VideoCapture cap(0);

	if (!cap.isOpened()) {
		cerr << "Camera open failed!" << endl;
		return;
	}

	QRCodeDetector detector;

	Mat frame;
	while (true) {
		cap >> frame;

		if (frame.empty()) {
			cerr << "Frame load failed!" << endl;
			break;
		}

		vector<Point> points;
		String info = detector.detectAndDecode(frame, points);

		if (!info.empty()) {
			polylines(frame, points, true, Scalar(0, 0, 255), 2);
			putText(frame, info, Point(10, 30), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 0, 255));
		}

		imshow("frame", frame);
		if (waitKey(1) == 27)
			break;
	}
}

C++ Code 출처 : OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝 - 황선규 저


 

'OpenCV' 카테고리의 다른 글

ch14 corners  (0) 2024.05.22
ch13 template  (0) 2024.05.21
ch13 hog  (0) 2024.05.21
ch13 cascade  (0) 2024.05.21
ch12 polygon  (0) 2024.05.21