Remove old scanner programs
|
@ -1,5 +0,0 @@
|
||||||
cmake_install.cmake
|
|
||||||
CMakeCache.txt
|
|
||||||
CMakeFiles/*
|
|
||||||
Makefile
|
|
||||||
AScannerDarkly
|
|
|
@ -1,7 +0,0 @@
|
||||||
cmake_minimum_required(VERSION 3.2)
|
|
||||||
project( AScannerDarkly )
|
|
||||||
find_package( OpenCV REQUIRED )
|
|
||||||
include_directories( ${OpenCV_INCLUDE_DIRS} )
|
|
||||||
add_executable( AScannerDarkly main.cpp )
|
|
||||||
target_link_libraries( AScannerDarkly ${OpenCV_LIBS} )
|
|
||||||
target_link_libraries( AScannerDarkly -llept -ltesseract )
|
|
Before Width: | Height: | Size: 5.2 KiB |
Before Width: | Height: | Size: 124 KiB |
Before Width: | Height: | Size: 126 KiB |
Before Width: | Height: | Size: 115 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 9.0 KiB |
Before Width: | Height: | Size: 4.7 KiB |
Before Width: | Height: | Size: 4.7 KiB |
Before Width: | Height: | Size: 4.8 KiB |
Before Width: | Height: | Size: 5.0 KiB |
|
@ -1,161 +0,0 @@
|
||||||
#include <opencv2/opencv.hpp>
|
|
||||||
#include <tesseract/baseapi.h>
|
|
||||||
#include <leptonica/allheaders.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
const std::string SCANNER_WINDOW_NAME = "A Scanner Darkly";
|
|
||||||
const std::string CARD_WINDOW_NAME = "Detected Card";
|
|
||||||
|
|
||||||
const std::string CANNY_LOWER_THRESHOLD_TRACKBAR_NAME = "Canny: Lower Threshold";
|
|
||||||
|
|
||||||
const float CARD_ASPECT_RATIO = 88.0f / 63.0f;
|
|
||||||
const std::string ASPECT_RATIO_TOLERANCE_TRACKBAR_NAME = "Aspect Ratio Tolerance";
|
|
||||||
int g_aspect_ratio_tolerance = 1;
|
|
||||||
const int MAX_ASPECT_RATIO_TOLERANCE = 100;
|
|
||||||
|
|
||||||
int g_Canny_lower_threshold = 195;
|
|
||||||
const int CANNY_UPPER_THRESHOLD = 255;
|
|
||||||
|
|
||||||
cv::Mat cropImageToRoi(cv::Mat img, cv::Rect roi) {
|
|
||||||
return img(roi);
|
|
||||||
}
|
|
||||||
|
|
||||||
cv::Mat detectCardInFrame(cv::Mat frame) {
|
|
||||||
cv::Mat grayscaleFrame, blurFrame, cannyFrame, cardFrame;
|
|
||||||
|
|
||||||
// Perform edge detection
|
|
||||||
cv::cvtColor(frame, grayscaleFrame, cv::COLOR_BGR2GRAY);
|
|
||||||
cv::GaussianBlur(grayscaleFrame, blurFrame, cv::Size(5, 5), 2, 2);
|
|
||||||
cv::Canny(blurFrame, cannyFrame, g_Canny_lower_threshold, CANNY_UPPER_THRESHOLD);
|
|
||||||
|
|
||||||
// Perform contour detection
|
|
||||||
std::vector<std::vector<cv::Point>> contours;
|
|
||||||
std::vector<cv::Vec4i> hierarchy;
|
|
||||||
cv::findContours(cannyFrame, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
|
|
||||||
|
|
||||||
// Detect convex hulls
|
|
||||||
std::vector<std::vector<cv::Point>> hull(contours.size());
|
|
||||||
for(size_t i = 0; i < contours.size(); i++) {
|
|
||||||
cv::convexHull(contours[i], hull[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Detect RotatedRects
|
|
||||||
std::vector<cv::RotatedRect> minRect(contours.size());
|
|
||||||
for(size_t i = 0; i < contours.size(); i++) {
|
|
||||||
minRect[i] = cv::minAreaRect(contours[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw lines around RotatedRects
|
|
||||||
/*
|
|
||||||
cv::Scalar rectangleColor(255, 0, 0, 0);
|
|
||||||
cv::Point2f rect_points[4];
|
|
||||||
minRect[i].points(rect_points);
|
|
||||||
for (int j = 0; j < 4; j++) {
|
|
||||||
cv::line(frame, rect_points[j], rect_points[(j + 1) % 4], rectangleColor);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Find possible cards
|
|
||||||
std::vector<cv::RotatedRect> possibleCards(contours.size());
|
|
||||||
int possibleCount = 0;
|
|
||||||
for (size_t i = 0; i < minRect.size(); i++) {
|
|
||||||
float aspectRatio = minRect[i].size.height / minRect[i].size.width;
|
|
||||||
float aspectRatioTolerance = g_aspect_ratio_tolerance / 100.0f;
|
|
||||||
if (aspectRatio < (CARD_ASPECT_RATIO - aspectRatioTolerance) || aspectRatio > (CARD_ASPECT_RATIO + aspectRatioTolerance)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
cv::putText(frame, std::to_string(minRect[i].angle), minRect[i].center, cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 0, 0, 0));
|
|
||||||
|
|
||||||
possibleCards[possibleCount] = minRect[i];
|
|
||||||
possibleCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (possibleCount == 1) {
|
|
||||||
return cropImageToRoi(frame, possibleCards[0].boundingRect());
|
|
||||||
} else if (possibleCount > 1) {
|
|
||||||
// If we have more than one possible match, take the largest one
|
|
||||||
int largestCardIndex = 0;
|
|
||||||
int largestCardSize = 0;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < possibleCards.size(); i++) {
|
|
||||||
float size = possibleCards[i].size.width * possibleCards[i].size.height;
|
|
||||||
if (size >= largestCardSize) {
|
|
||||||
largestCardIndex = i;
|
|
||||||
largestCardSize = size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Some basic error checking to ensure the RotatedRect is roughly card-sized
|
|
||||||
if (largestCardSize >= 14400 && largestCardSize <= 200000) {
|
|
||||||
return cropImageToRoi(frame, possibleCards[largestCardIndex].boundingRect());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cv::Mat();
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* readTextFromImage(cv::InputArray img) {
|
|
||||||
const char* tempFilename = "temp.bmp";
|
|
||||||
|
|
||||||
// Initialize Tesseract api
|
|
||||||
tesseract::TessBaseAPI* tess = new tesseract::TessBaseAPI();
|
|
||||||
tess->Init(NULL, "eng");
|
|
||||||
tess->SetPageSegMode(tesseract::PSM_SPARSE_TEXT);
|
|
||||||
|
|
||||||
// Load image into Tesseract
|
|
||||||
cv::imwrite(tempFilename, img);
|
|
||||||
Pix* pixd = pixRead(tempFilename);
|
|
||||||
tess->SetImage(pixd);
|
|
||||||
tess->Recognize(0);
|
|
||||||
|
|
||||||
// Perform OCR
|
|
||||||
const char* out = tess->GetUTF8Text();
|
|
||||||
|
|
||||||
// Cleanup
|
|
||||||
pixDestroy(&pixd);
|
|
||||||
std::remove(tempFilename);
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv ) {
|
|
||||||
cv::VideoCapture cap;
|
|
||||||
cap.open(0);
|
|
||||||
|
|
||||||
if (!cap.isOpened()) {
|
|
||||||
std::cerr << "Couldn't open capture" << std::endl;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
cv::namedWindow(SCANNER_WINDOW_NAME);
|
|
||||||
|
|
||||||
cv::createTrackbar(CANNY_LOWER_THRESHOLD_TRACKBAR_NAME, SCANNER_WINDOW_NAME, &g_Canny_lower_threshold, CANNY_UPPER_THRESHOLD, NULL);
|
|
||||||
cv::createTrackbar(ASPECT_RATIO_TOLERANCE_TRACKBAR_NAME, SCANNER_WINDOW_NAME, &g_aspect_ratio_tolerance, MAX_ASPECT_RATIO_TOLERANCE, NULL);
|
|
||||||
|
|
||||||
cv::Mat frame, cardFrame;
|
|
||||||
while (true) {
|
|
||||||
cap >> frame;
|
|
||||||
//cardFrame = detectCardInFrame(frame);
|
|
||||||
cv::imshow(SCANNER_WINDOW_NAME, frame);
|
|
||||||
|
|
||||||
/*
|
|
||||||
if (cardFrame.size().width > 0) {
|
|
||||||
printf("Card detected\n");
|
|
||||||
while (true) {
|
|
||||||
cv::imshow(CARD_WINDOW_NAME, cardFrame);
|
|
||||||
char c = (char)cv::waitKey(33);
|
|
||||||
if (c == 27) {
|
|
||||||
cv::destroyWindow(CARD_WINDOW_NAME);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
char c = (char)cv::waitKey(33);
|
|
||||||
if (c == 27) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Before Width: | Height: | Size: 301 KiB |
|
@ -1,24 +0,0 @@
|
||||||
#!/usr/bin/python3
|
|
||||||
|
|
||||||
import cv2
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
mouse_raw = (0, 0)
|
|
||||||
|
|
||||||
def clickEvent(event, x, y, flags, params):
|
|
||||||
global mouse_raw
|
|
||||||
|
|
||||||
if event == cv2.EVENT_LBUTTONDOWN:
|
|
||||||
mouse_raw = (x, y)
|
|
||||||
print(mouse_raw)
|
|
||||||
|
|
||||||
cap = cv2.VideoCapture(0)
|
|
||||||
cv2.namedWindow("image")
|
|
||||||
cv2.setMouseCallback("image", clickEvent)
|
|
||||||
|
|
||||||
while True:
|
|
||||||
success, img = cap.read()
|
|
||||||
cv2.imshow("image", img)
|
|
||||||
cv2.waitKey(0)
|
|
||||||
|
|
||||||
cv2.destroyAllWindows()
|
|