Merge remote-tracking branch 'upstream/3.4' into merge-3.4

This commit is contained in:
Alexander Alekhin
2020-01-28 13:16:03 +03:00
40 changed files with 3133 additions and 740 deletions
+246 -86
View File
@@ -2,23 +2,45 @@
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/imgcodecs.hpp"
#include <string>
#include <iostream>
using namespace std;
using namespace cv;
static void drawQRCodeContour(Mat &color_image, vector<Point> transform);
static void drawFPS(Mat &color_image, double fps);
static int liveQRCodeDetect(const string& out_file);
static int imageQRCodeDetect(const string& in_file, const string& out_file);
static int liveQRCodeDetect();
static int imageQRCodeDetect(const string& in_file);
static bool g_modeMultiQR = false;
static bool g_detectOnly = false;
static string g_out_file_name, g_out_file_ext;
static int g_save_idx = 0;
static bool g_saveDetections = false;
static bool g_saveAll = false;
static string getQRModeString()
{
std::ostringstream out;
out << "QR"
<< (g_modeMultiQR ? " multi" : "")
<< (g_detectOnly ? " detector" : " decoder");
return out.str();
}
int main(int argc, char *argv[])
{
const string keys =
"{h help ? | | print help messages }"
"{i in | | input path to file for detect (with parameter - show image, otherwise - camera)}"
"{o out | | output path to file (save image, work with -i parameter) }";
"{i in | | input image path (also switches to image detection mode) }"
"{detect | false | detect QR code only (skip decoding) }"
"{m multi | | use detect for multiple qr-codes }"
"{o out | qr_code.png | path to result file }"
"{save_detections | false | save all QR detections (video mode only) }"
"{save_all | false | save all processed frames (video mode only) }"
;
CommandLineParser cmd_parser(argc, argv, keys);
cmd_parser.about("This program detects the QR-codes from camera or images using the OpenCV library.");
@@ -28,32 +50,51 @@ int main(int argc, char *argv[])
return 0;
}
string in_file_name = cmd_parser.get<string>("in"); // input path to image
string out_file_name;
if (cmd_parser.has("out"))
out_file_name = cmd_parser.get<string>("out"); // output path to image
string in_file_name = cmd_parser.get<string>("in"); // path to input image
if (cmd_parser.has("out"))
{
std::string fpath = cmd_parser.get<string>("out"); // path to output image
std::string::size_type idx = fpath.rfind('.');
if (idx != std::string::npos)
{
g_out_file_name = fpath.substr(0, idx);
g_out_file_ext = fpath.substr(idx);
}
else
{
g_out_file_name = fpath;
g_out_file_ext = ".png";
}
}
if (!cmd_parser.check())
{
cmd_parser.printErrors();
return -1;
}
g_modeMultiQR = cmd_parser.has("multi") && cmd_parser.get<bool>("multi");
g_detectOnly = cmd_parser.has("detect") && cmd_parser.get<bool>("detect");
g_saveDetections = cmd_parser.has("save_detections") && cmd_parser.get<bool>("save_detections");
g_saveAll = cmd_parser.has("save_all") && cmd_parser.get<bool>("save_all");
int return_code = 0;
if (in_file_name.empty())
{
return_code = liveQRCodeDetect(out_file_name);
return_code = liveQRCodeDetect();
}
else
{
return_code = imageQRCodeDetect(samples::findFile(in_file_name), out_file_name);
return_code = imageQRCodeDetect(samples::findFile(in_file_name));
}
return return_code;
}
void drawQRCodeContour(Mat &color_image, vector<Point> transform)
static
void drawQRCodeContour(Mat &color_image, const vector<Point>& corners)
{
if (!transform.empty())
if (!corners.empty())
{
double show_radius = (color_image.rows > color_image.cols)
? (2.813 * color_image.rows) / color_image.cols
@@ -61,127 +102,246 @@ void drawQRCodeContour(Mat &color_image, vector<Point> transform)
double contour_radius = show_radius * 0.4;
vector< vector<Point> > contours;
contours.push_back(transform);
contours.push_back(corners);
drawContours(color_image, contours, 0, Scalar(211, 0, 148), cvRound(contour_radius));
RNG rng(1000);
for (size_t i = 0; i < 4; i++)
{
Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
circle(color_image, transform[i], cvRound(show_radius), color, -1);
circle(color_image, corners[i], cvRound(show_radius), color, -1);
}
}
}
static
void drawFPS(Mat &color_image, double fps)
{
ostringstream convert;
convert << cvRound(fps) << " FPS (QR detection)";
convert << cv::format("%.2f", fps) << " FPS (" << getQRModeString() << ")";
putText(color_image, convert.str(), Point(25, 25), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 0, 255), 2);
}
int liveQRCodeDetect(const string& out_file)
static
void drawQRCodeResults(Mat& frame, const vector<Point>& corners, const vector<cv::String>& decode_info, double fps)
{
VideoCapture cap(0);
if(!cap.isOpened())
if (!corners.empty())
{
cout << "Cannot open a camera" << endl;
return -4;
for (size_t i = 0; i < corners.size(); i += 4)
{
size_t qr_idx = i / 4;
vector<Point> qrcode_contour(corners.begin() + i, corners.begin() + i + 4);
drawQRCodeContour(frame, qrcode_contour);
cout << "QR[" << qr_idx << "] @ " << Mat(qrcode_contour).reshape(2, 1) << ": ";
if (decode_info.size() > qr_idx)
{
if (!decode_info[qr_idx].empty())
cout << "'" << decode_info[qr_idx] << "'" << endl;
else
cout << "can't decode QR code" << endl;
}
else
{
cout << "decode information is not available (disabled)" << endl;
}
}
}
else
{
cout << "QR code is not detected" << endl;
}
QRCodeDetector qrcode;
TickMeter total;
for(;;)
drawFPS(frame, fps);
}
static
void runQR(
QRCodeDetector& qrcode, const Mat& input,
vector<Point>& corners, vector<cv::String>& decode_info
// +global: bool g_modeMultiQR, bool g_detectOnly
)
{
if (!g_modeMultiQR)
{
Mat frame, src, straight_barcode;
string decode_info;
vector<Point> transform;
if (!g_detectOnly)
{
String decode_info1 = qrcode.detectAndDecode(input, corners);
decode_info.push_back(decode_info1);
}
else
{
bool detection_result = qrcode.detect(input, corners);
CV_UNUSED(detection_result);
}
}
else
{
if (!g_detectOnly)
{
bool result_detection = qrcode.detectAndDecodeMulti(input, decode_info, corners);
CV_UNUSED(result_detection);
}
else
{
bool result_detection = qrcode.detectMulti(input, corners);
CV_UNUSED(result_detection);
}
}
}
static
double processQRCodeDetection(QRCodeDetector& qrcode, const Mat& input, Mat& result, vector<Point>& corners)
{
if (input.channels() == 1)
cvtColor(input, result, COLOR_GRAY2BGR);
else
input.copyTo(result);
cout << "Run " << getQRModeString()
<< " on image: " << input.size() << " (" << typeToString(input.type()) << ")"
<< endl;
TickMeter timer;
vector<cv::String> decode_info;
timer.start();
runQR(qrcode, input, corners, decode_info);
timer.stop();
double fps = 1 / timer.getTimeSec();
drawQRCodeResults(result, corners, decode_info, fps);
return fps;
}
int liveQRCodeDetect()
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Cannot open a camera" << endl;
return 2;
}
cout << "Press 'm' to switch between detectAndDecode and detectAndDecodeMulti" << endl;
cout << "Press 'd' to switch between decoder and detector" << endl;
cout << "Press ' ' (space) to save result into images" << endl;
cout << "Press 'ESC' to exit" << endl;
QRCodeDetector qrcode;
for (;;)
{
Mat frame;
cap >> frame;
if (frame.empty())
{
cout << "End of video stream" << endl;
break;
}
cvtColor(frame, src, COLOR_BGR2GRAY);
total.start();
bool result_detection = qrcode.detect(src, transform);
if (result_detection)
bool forceSave = g_saveAll;
Mat result;
try
{
decode_info = qrcode.decode(src, transform, straight_barcode);
if (!decode_info.empty()) { cout << decode_info << endl; }
vector<Point> corners;
double fps = processQRCodeDetection(qrcode, frame, result, corners);
cout << "FPS: " << fps << endl;
forceSave |= (g_saveDetections && !corners.empty());
//forceSave |= fps < 1.0;
}
catch (const cv::Exception& e)
{
cerr << "ERROR exception: " << e.what() << endl;
forceSave = true;
}
total.stop();
double fps = 1 / total.getTimeSec();
total.reset();
if (result_detection) { drawQRCodeContour(frame, transform); }
drawFPS(frame, fps);
if (!result.empty())
imshow("QR code", result);
imshow("Live QR code detector", frame);
char c = (char)waitKey(30);
int code = waitKey(1);
if (code < 0 && !forceSave)
continue; // timeout
char c = (char)code;
if (c == ' ' || forceSave)
{
string fsuffix = cv::format("-%05d", g_save_idx++);
string fname_input = g_out_file_name + fsuffix + "_input.png";
cout << "Saving QR code detection input: '" << fname_input << "' ..." << endl;
imwrite(fname_input, frame);
string fname = g_out_file_name + fsuffix + g_out_file_ext;
cout << "Saving QR code detection result: '" << fname << "' ..." << endl;
imwrite(fname, result);
cout << "Saved" << endl;
}
if (c == 'm')
{
g_modeMultiQR = !g_modeMultiQR;
cout << "Switching QR code mode ==> " << (g_modeMultiQR ? "detectAndDecodeMulti" : "detectAndDecode") << endl;
}
if (c == 'd')
{
g_detectOnly = !g_detectOnly;
cout << "Switching QR decoder mode ==> " << (g_detectOnly ? "detect" : "decode") << endl;
}
if (c == 27)
{
cout << "'ESC' is pressed. Exiting..." << endl;
break;
if (c == ' ' && !out_file.empty())
imwrite(out_file, frame); // TODO write original frame too
}
}
cout << "Exit." << endl;
return 0;
}
int imageQRCodeDetect(const string& in_file, const string& out_file)
int imageQRCodeDetect(const string& in_file)
{
Mat color_src = imread(in_file, IMREAD_COLOR), src;
cvtColor(color_src, src, COLOR_BGR2GRAY);
Mat straight_barcode;
string decoded_info;
vector<Point> transform;
const int count_experiments = 10;
double transform_time = 0.0;
bool result_detection = false;
TickMeter total;
Mat input = imread(in_file, IMREAD_COLOR);
cout << "Run " << getQRModeString()
<< " on image: " << input.size() << " (" << typeToString(input.type()) << ")"
<< endl;
QRCodeDetector qrcode;
vector<Point> corners;
vector<cv::String> decode_info;
TickMeter timer;
for (size_t i = 0; i < count_experiments; i++)
{
total.start();
transform.clear();
result_detection = qrcode.detect(src, transform);
total.stop();
transform_time += total.getTimeSec();
total.reset();
if (!result_detection)
continue;
corners.clear();
decode_info.clear();
total.start();
decoded_info = qrcode.decode(src, transform, straight_barcode);
total.stop();
transform_time += total.getTimeSec();
total.reset();
timer.start();
runQR(qrcode, input, corners, decode_info);
timer.stop();
}
double fps = count_experiments / transform_time;
if (!result_detection)
cout << "QR code not found" << endl;
if (decoded_info.empty())
cout << "QR code cannot be decoded" << endl;
drawQRCodeContour(color_src, transform);
drawFPS(color_src, fps);
cout << "Input image file path: " << in_file << endl;
cout << "Output image file path: " << out_file << endl;
cout << "Size: " << color_src.size() << endl;
double fps = count_experiments / timer.getTimeSec();
cout << "FPS: " << fps << endl;
cout << "Decoded info: " << decoded_info << endl;
if (!out_file.empty())
Mat result; input.copyTo(result);
drawQRCodeResults(result, corners, decode_info, fps);
imshow("QR", result); waitKey(1);
if (!g_out_file_name.empty())
{
imwrite(out_file, color_src);
string out_file = g_out_file_name + g_out_file_ext;
cout << "Saving result: " << out_file << endl;
imwrite(out_file, result);
}
for(;;)
{
imshow("Detect QR code on image", color_src);
if (waitKey(0) == 27)
break;
}
cout << "Press any key to exit ..." << endl;
waitKey(0);
cout << "Exit." << endl;
return 0;
}
+74
View File
@@ -0,0 +1,74 @@
/*
This tutorial demonstrates how to correct the skewness in a text.
The program takes as input a skewed source image and shows non skewed text.
*/
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <iomanip>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
CommandLineParser parser(argc, argv, "{@input | imageTextR.png | input image}");
// Load image from the disk
Mat image = imread( samples::findFile( parser.get<String>("@input") ), IMREAD_COLOR);
if (image.empty())
{
cout << "Cannot load the image " + parser.get<String>("@input") << endl;
return -1;
}
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
//Threshold the image, setting all foreground pixels to 255 and all background pixels to 0
Mat thresh;
threshold(gray, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
// Applying erode filter to remove random noise
int erosion_size = 1;
Mat element = getStructuringElement( MORPH_RECT, Size(2*erosion_size+1, 2*erosion_size+1), Point(erosion_size, erosion_size) );
erode(thresh, thresh, element);
cv::Mat coords;
findNonZero(thresh, coords);
RotatedRect box = minAreaRect(coords);
float angle = box.angle;
// The cv::minAreaRect function returns values in the range [-90, 0)
// if the angle is less than -45 we need to add 90 to it
if (angle < -45.0f)
{
angle = (90.0f + angle);
}
//Obtaining the rotation matrix
Point2f center((image.cols) / 2.0f, (image.rows) / 2.0f);
Mat M = getRotationMatrix2D(center, angle, 1.0f);
Mat rotated;
// Rotating the image by required angle
stringstream angle_to_str;
angle_to_str << fixed << setprecision(2) << angle;
warpAffine(image, rotated, M, image.size(), INTER_CUBIC, BORDER_REPLICATE);
putText(rotated, "Angle " + angle_to_str.str() + " degrees", Point(10, 30), FONT_HERSHEY_SIMPLEX, 0.7, Scalar(0, 0, 255), 2);
cout << "[INFO] angle: " << angle_to_str.str() << endl;
//Show the image
imshow("Input", image);
imshow("Rotated", rotated);
waitKey(0);
return 0;
}
+178
View File
@@ -0,0 +1,178 @@
#!/usr/bin/env python
'''
You can download the converted pb model from https://www.dropbox.com/s/qag9vzambhhkvxr/lip_jppnet_384.pb?dl=0
or convert the model yourself.
Follow these steps if you want to convert the original model yourself:
To get original .meta pre-trained model download https://drive.google.com/file/d/1BFVXgeln-bek8TCbRjN6utPAgRE0LJZg/view
For correct convert .meta to .pb model download original repository https://github.com/Engineering-Course/LIP_JPPNet
Change script evaluate_parsing_JPPNet-s2.py for human parsing
1. Remove preprocessing to create image_batch_origin:
with tf.name_scope("create_inputs"):
...
Add
image_batch_origin = tf.placeholder(tf.float32, shape=(2, None, None, 3), name='input')
2. Create input
image = cv2.imread(path/to/image)
image_rev = np.flip(image, axis=1)
input = np.stack([image, image_rev], axis=0)
3. Hardcode image_h and image_w shapes to determine output shapes.
We use default INPUT_SIZE = (384, 384) from evaluate_parsing_JPPNet-s2.py.
parsing_out1 = tf.reduce_mean(tf.stack([tf.image.resize_images(parsing_out1_100, INPUT_SIZE),
tf.image.resize_images(parsing_out1_075, INPUT_SIZE),
tf.image.resize_images(parsing_out1_125, INPUT_SIZE)]), axis=0)
Do similarly with parsing_out2, parsing_out3
4. Remove postprocessing. Last net operation:
raw_output = tf.reduce_mean(tf.stack([parsing_out1, parsing_out2, parsing_out3]), axis=0)
Change:
parsing_ = sess.run(raw_output, feed_dict={'input:0': input})
5. To save model after sess.run(...) add:
input_graph_def = tf.get_default_graph().as_graph_def()
output_node = "Mean_3"
output_graph_def = tf.graph_util.convert_variables_to_constants(sess, input_graph_def, output_node)
output_graph = "LIP_JPPNet.pb"
with tf.gfile.GFile(output_graph, "wb") as f:
f.write(output_graph_def.SerializeToString())'
'''
import argparse
import numpy as np
import cv2 as cv
backends = (cv.dnn.DNN_BACKEND_DEFAULT, cv.dnn.DNN_BACKEND_INFERENCE_ENGINE, cv.dnn.DNN_BACKEND_OPENCV)
targets = (cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_OPENCL, cv.dnn.DNN_TARGET_OPENCL_FP16, cv.dnn.DNN_TARGET_MYRIAD)
def preprocess(image_path):
"""
Create 4-dimensional blob from image and flip image
:param image_path: path to input image
"""
image = cv.imread(image_path)
image_rev = np.flip(image, axis=1)
input = cv.dnn.blobFromImages([image, image_rev], mean=(104.00698793, 116.66876762, 122.67891434))
return input
def run_net(input, model_path, backend, target):
"""
Read network and infer model
:param model_path: path to JPPNet model
:param backend: computation backend
:param target: computation device
"""
net = cv.dnn.readNet(model_path)
net.setPreferableBackend(backend)
net.setPreferableTarget(target)
net.setInput(input)
out = net.forward()
return out
def postprocess(out, input_shape):
"""
Create a grayscale human segmentation
:param out: network output
:param input_shape: input image width and height
"""
# LIP classes
# 0 Background
# 1 Hat
# 2 Hair
# 3 Glove
# 4 Sunglasses
# 5 UpperClothes
# 6 Dress
# 7 Coat
# 8 Socks
# 9 Pants
# 10 Jumpsuits
# 11 Scarf
# 12 Skirt
# 13 Face
# 14 LeftArm
# 15 RightArm
# 16 LeftLeg
# 17 RightLeg
# 18 LeftShoe
# 19 RightShoe
head_output, tail_output = np.split(out, indices_or_sections=[1], axis=0)
head_output = head_output.squeeze(0)
tail_output = tail_output.squeeze(0)
head_output = np.stack([cv.resize(img, dsize=input_shape) for img in head_output[:, ...]])
tail_output = np.stack([cv.resize(img, dsize=input_shape) for img in tail_output[:, ...]])
tail_list = np.split(tail_output, indices_or_sections=list(range(1, 20)), axis=0)
tail_list = [arr.squeeze(0) for arr in tail_list]
tail_list_rev = [tail_list[i] for i in range(14)]
tail_list_rev.extend([tail_list[15], tail_list[14], tail_list[17], tail_list[16], tail_list[19], tail_list[18]])
tail_output_rev = np.stack(tail_list_rev, axis=0)
tail_output_rev = np.flip(tail_output_rev, axis=2)
raw_output_all = np.mean(np.stack([head_output, tail_output_rev], axis=0), axis=0, keepdims=True)
raw_output_all = np.argmax(raw_output_all, axis=1)
raw_output_all = raw_output_all.transpose(1, 2, 0)
return raw_output_all
def decode_labels(gray_image):
"""
Colorize image according to labels
:param gray_image: grayscale human segmentation result
"""
height, width, _ = gray_image.shape
colors = [(0, 0, 0), (128, 0, 0), (255, 0, 0), (0, 85, 0), (170, 0, 51), (255, 85, 0),
(0, 0, 85), (0, 119, 221), (85, 85, 0), (0, 85, 85), (85, 51, 0), (52, 86, 128),
(0, 128, 0), (0, 0, 255), (51, 170, 221), (0, 255, 255),(85, 255, 170),
(170, 255, 85), (255, 255, 0), (255, 170, 0)]
segm = np.stack([colors[idx] for idx in gray_image.flatten()])
segm = segm.reshape(height, width, 3).astype(np.uint8)
segm = cv.cvtColor(segm, cv.COLOR_BGR2RGB)
return segm
def parse_human(image_path, model_path, backend=cv.dnn.DNN_BACKEND_OPENCV, target=cv.dnn.DNN_TARGET_CPU):
"""
Prepare input for execution, run net and postprocess output to parse human.
:param image_path: path to input image
:param model_path: path to JPPNet model
:param backend: name of computation backend
:param target: name of computation target
"""
input = preprocess(image_path)
input_h, input_w = input.shape[2:]
output = run_net(input, model_path, backend, target)
grayscale_out = postprocess(output, (input_w, input_h))
segmentation = decode_labels(grayscale_out)
return segmentation
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Use this script to run human parsing using JPPNet',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--input', '-i', required=True, help='Path to input image.')
parser.add_argument('--model', '-m', required=True, help='Path to pb model.')
parser.add_argument('--backend', choices=backends, default=cv.dnn.DNN_BACKEND_DEFAULT, type=int,
help="Choose one of computation backends: "
"%d: automatically (by default), "
"%d: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
"%d: OpenCV implementation" % backends)
parser.add_argument('--target', choices=targets, default=cv.dnn.DNN_TARGET_CPU, type=int,
help='Choose one of target computation devices: '
'%d: CPU target (by default), '
'%d: OpenCL, '
'%d: OpenCL fp16 (half-float precision), '
'%d: VPU' % targets)
args, _ = parser.parse_known_args()
output = parse_human(args.input, args.model, args.backend, args.target)
winName = 'Deep learning human parsing in OpenCV'
cv.namedWindow(winName, cv.WINDOW_AUTOSIZE)
cv.imshow(winName, output)
cv.waitKey()
+4 -3
View File
@@ -43,6 +43,7 @@ parser.add_argument('--target', choices=targets, default=cv.dnn.DNN_TARGET_CPU,
'%d: OpenCL fp16 (half-float precision), '
'%d: VPU' % targets)
parser.add_argument('--async', type=int, default=0,
dest='asyncN',
help='Number of asynchronous forwards at the same time. '
'Choose 0 for synchronous mode')
args, _ = parser.parse_known_args()
@@ -231,8 +232,8 @@ def processingThreadBody():
try:
frame = framesQueue.get_nowait()
if args.async:
if len(futureOutputs) == args.async:
if args.asyncN:
if len(futureOutputs) == args.asyncN:
frame = None # Skip the frame
else:
framesQueue.queue.clear() # Skip the rest of frames
@@ -256,7 +257,7 @@ def processingThreadBody():
frame = cv.resize(frame, (inpWidth, inpHeight))
net.setInput(np.array([[inpHeight, inpWidth, 1.6]], dtype=np.float32), 'im_info')
if args.async:
if args.asyncN:
futureOutputs.append(net.forwardAsync())
else:
outs = net.forward(outNames)
@@ -0,0 +1,58 @@
'''
Text skewness correction
This tutorial demonstrates how to correct the skewness in a text.
The program takes as input a skewed source image and shows non skewed text.
Usage:
python text_skewness_correction.py --image "Image path"
'''
import numpy as np
import cv2 as cv
import sys
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--image", required=True, help="path to input image file")
args = vars(parser.parse_args())
# load the image from disk
image = cv.imread(cv.samples.findFile(args["image"]))
if image is None:
print("can't read image " + args["image"])
sys.exit(-1)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# threshold the image, setting all foreground pixels to
# 255 and all background pixels to 0
thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)[1]
# Applying erode filter to remove random noise
erosion_size = 1
element = cv.getStructuringElement(cv.MORPH_RECT, (2 * erosion_size + 1, 2 * erosion_size + 1), (erosion_size, erosion_size) )
thresh = cv.erode(thresh, element)
coords = cv.findNonZero(thresh)
angle = cv.minAreaRect(coords)[-1]
# the `cv.minAreaRect` function returns values in the
# range [-90, 0) if the angle is less than -45 we need to add 90 to it
if angle < -45:
angle = (90 + angle)
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv.getRotationMatrix2D(center, angle, 1.0)
rotated = cv.warpAffine(image, M, (w, h), flags=cv.INTER_CUBIC, borderMode=cv.BORDER_REPLICATE)
cv.putText(rotated, "Angle: {:.2f} degrees".format(angle), (10, 30), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# show the output image
print("[INFO] angle: {:.2f}".format(angle))
cv.imshow("Input", image)
cv.imshow("Rotated", rotated)
cv.waitKey(0)
if __name__ == "__main__":
main()