modified data matrix detection interface (that does not use any new data structures). Added Python sample for data matrix detection. Ticket #1664
This commit is contained in:
@@ -21,80 +21,81 @@ using namespace std;
|
||||
//hide the local functions in an anon namespace
|
||||
namespace
|
||||
{
|
||||
void help(char** av)
|
||||
{
|
||||
cout << "\nThis program justs gets you started reading images from video\n"
|
||||
"Usage:\n./" << av[0] << " <video device number>\n" << "q,Q,esc -- quit\n"
|
||||
<< "space -- save frame\n\n"
|
||||
<< "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
|
||||
<< "\tThe program captures frames from a camera connected to your computer.\n"
|
||||
<< "\tTo find the video device number, try ls /dev/video* \n"
|
||||
<< "\tYou may also pass a video file, like my_vide.avi instead of a device number"
|
||||
<< "\n"
|
||||
<< "DATA:\n"
|
||||
<< "Generate a datamatrix from from http://datamatrix.kaywa.com/ \n"
|
||||
<< " NOTE: This only handles strings of len 3 or less\n"
|
||||
<< " Resize the screen to be large enough for your camera to see, and it should find an read it.\n\n"
|
||||
<< endl;
|
||||
}
|
||||
|
||||
int process(VideoCapture& capture)
|
||||
{
|
||||
std::vector<DataMatrixCode> codes;
|
||||
int n = 0;
|
||||
char filename[200];
|
||||
string window_name = "video | q or esc to quit";
|
||||
cout << "press space to save a picture. q or esc to quit" << endl;
|
||||
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
|
||||
Mat frame;
|
||||
for (;;)
|
||||
{
|
||||
capture >> frame;
|
||||
if (frame.empty())
|
||||
break;
|
||||
cv::Mat gray;
|
||||
cv::cvtColor(frame,gray,CV_RGB2GRAY);
|
||||
findDataMatrix(gray, codes);
|
||||
drawDataMatrixCodes(codes, frame);
|
||||
imshow(window_name, frame);
|
||||
char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
|
||||
switch (key)
|
||||
void help(char** av)
|
||||
{
|
||||
case 'q':
|
||||
case 'Q':
|
||||
case 27: //escape key
|
||||
return 0;
|
||||
case ' ': //Save an image
|
||||
sprintf(filename, "filename%.3d.jpg", n++);
|
||||
imwrite(filename, frame);
|
||||
cout << "Saved " << filename << endl;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
cout << "\nThis program justs gets you started reading images from video\n"
|
||||
"Usage:\n./" << av[0] << " <video device number>\n" << "q,Q,esc -- quit\n"
|
||||
<< "space -- save frame\n\n"
|
||||
<< "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
|
||||
<< "\tThe program captures frames from a camera connected to your computer.\n"
|
||||
<< "\tTo find the video device number, try ls /dev/video* \n"
|
||||
<< "\tYou may also pass a video file, like my_vide.avi instead of a device number"
|
||||
<< "\n"
|
||||
<< "DATA:\n"
|
||||
<< "Generate a datamatrix from from http://datamatrix.kaywa.com/ \n"
|
||||
<< " NOTE: This only handles strings of len 3 or less\n"
|
||||
<< " Resize the screen to be large enough for your camera to see, and it should find an read it.\n\n"
|
||||
<< endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int process(VideoCapture& capture)
|
||||
{
|
||||
int n = 0;
|
||||
char filename[200];
|
||||
string window_name = "video | q or esc to quit";
|
||||
cout << "press space to save a picture. q or esc to quit" << endl;
|
||||
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
|
||||
Mat frame;
|
||||
for (;;)
|
||||
{
|
||||
capture >> frame;
|
||||
if (frame.empty())
|
||||
break;
|
||||
cv::Mat gray;
|
||||
cv::cvtColor(frame,gray,CV_RGB2GRAY);
|
||||
vector<string> codes;
|
||||
Mat corners;
|
||||
findDataMatrix(gray, codes, corners);
|
||||
drawDataMatrixCodes(frame, codes, corners);
|
||||
imshow(window_name, frame);
|
||||
char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
|
||||
switch (key)
|
||||
{
|
||||
case 'q':
|
||||
case 'Q':
|
||||
case 27: //escape key
|
||||
return 0;
|
||||
case ' ': //Save an image
|
||||
sprintf(filename, "filename%.3d.jpg", n++);
|
||||
imwrite(filename, frame);
|
||||
cout << "Saved " << filename << endl;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int ac, char** av)
|
||||
{
|
||||
|
||||
if (ac != 2)
|
||||
{
|
||||
help(av);
|
||||
return 1;
|
||||
}
|
||||
std::string arg = av[1];
|
||||
VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
|
||||
if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
|
||||
capture.open(atoi(arg.c_str()));
|
||||
if (!capture.isOpened())
|
||||
{
|
||||
cerr << "Failed to open a video device or video file!\n" << endl;
|
||||
help(av);
|
||||
return 1;
|
||||
}
|
||||
return process(capture);
|
||||
|
||||
if (ac != 2)
|
||||
{
|
||||
help(av);
|
||||
return 1;
|
||||
}
|
||||
std::string arg = av[1];
|
||||
VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
|
||||
if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
|
||||
capture.open(atoi(arg.c_str()));
|
||||
if (!capture.isOpened())
|
||||
{
|
||||
cerr << "Failed to open a video device or video file!\n" << endl;
|
||||
help(av);
|
||||
return 1;
|
||||
}
|
||||
return process(capture);
|
||||
}
|
||||
|
68
samples/python2/video_dmtx.py
Normal file
68
samples/python2/video_dmtx.py
Normal file
@@ -0,0 +1,68 @@
|
||||
help='''
|
||||
Data matrix detector sample.
|
||||
Usage:
|
||||
video_dmtx {<video device number>|<video file name>}
|
||||
|
||||
Generate a datamatrix from from http://datamatrix.kaywa.com/ and print it out.
|
||||
NOTE: This only handles data matrices, generated for text strings of max 3 characters
|
||||
|
||||
Resize the screen to be large enough for your camera to see, and it should find an read it.
|
||||
|
||||
Keyboard shortcuts:
|
||||
|
||||
q or ESC - exit
|
||||
space - save current image as datamatrix<frame_number>.jpg
|
||||
'''
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import sys
|
||||
|
||||
def data_matrix_demo(cap):
|
||||
window_name = "Data Matrix Detector"
|
||||
frame_number = 0
|
||||
need_to_save = False
|
||||
|
||||
while 1:
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
||||
codes, corners, dmtx = cv2.findDataMatrix(gray)
|
||||
|
||||
cv2.drawDataMatrixCodes(frame, codes, corners)
|
||||
cv2.imshow(window_name, frame)
|
||||
|
||||
key = cv2.waitKey(30)
|
||||
c = chr(key & 255)
|
||||
if c in ['q', 'Q', chr(27)]:
|
||||
break
|
||||
|
||||
if c == ' ':
|
||||
need_to_save = True
|
||||
|
||||
if need_to_save and codes:
|
||||
filename = ("datamatrix%03d.jpg" % frame_number)
|
||||
cv2.imwrite(filename, frame)
|
||||
print "Saved frame to " + filename
|
||||
need_to_save = False
|
||||
|
||||
frame_number += 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print help
|
||||
|
||||
if len(sys.argv) == 1:
|
||||
cap = cv2.VideoCapture(0)
|
||||
else:
|
||||
cap = cv2.VideoCapture(sys.argv[1])
|
||||
if not cap.isOpened():
|
||||
cap = cv2.VideoCapture(int(sys.argv[1]))
|
||||
|
||||
if not cap.isOpened():
|
||||
print 'Cannot initialize video capture'
|
||||
sys.exit(-1)
|
||||
|
||||
data_matrix_demo(cap)
|
Reference in New Issue
Block a user