1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-25 05:13:04 +04:00

Merge branch 4.x

This commit is contained in:
Alexander Alekhin
2021-04-09 10:30:38 +00:00
1114 changed files with 64039 additions and 14611 deletions
@@ -38,10 +38,10 @@ def Hist_and_Backproj(val):
## [Read the image]
parser = argparse.ArgumentParser(description='Code for Back Projection tutorial.')
parser.add_argument('--input', help='Path to input image.')
parser.add_argument('--input', help='Path to input image.', default='home.jpg')
args = parser.parse_args()
src = cv.imread(args.input)
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
print('Could not open or find the image:', args.input)
exit(0)
@@ -54,10 +54,10 @@ def Hist_and_Backproj(mask):
# Read the image
parser = argparse.ArgumentParser(description='Code for Back Projection tutorial.')
parser.add_argument('--input', help='Path to input image.')
parser.add_argument('--input', help='Path to input image.', default='home.jpg')
args = parser.parse_args()
src = cv.imread(args.input)
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
print('Could not open or find the image:', args.input)
exit(0)
@@ -53,14 +53,14 @@ cv.normalize(r_hist, r_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
## [Draw for each channel]
for i in range(1, histSize):
cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(b_hist[i-1])) ),
( bin_w*(i), hist_h - int(round(b_hist[i])) ),
cv.line(histImage, ( bin_w*(i-1), hist_h - int(b_hist[i-1]) ),
( bin_w*(i), hist_h - int(b_hist[i]) ),
( 255, 0, 0), thickness=2)
cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(g_hist[i-1])) ),
( bin_w*(i), hist_h - int(round(g_hist[i])) ),
cv.line(histImage, ( bin_w*(i-1), hist_h - int(g_hist[i-1]) ),
( bin_w*(i), hist_h - int(g_hist[i]) ),
( 0, 255, 0), thickness=2)
cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(r_hist[i-1])) ),
( bin_w*(i), hist_h - int(round(r_hist[i])) ),
cv.line(histImage, ( bin_w*(i-1), hist_h - int(r_hist[i-1]) ),
( bin_w*(i), hist_h - int(r_hist[i]) ),
( 0, 0, 255), thickness=2)
## [Draw for each channel]
@@ -102,7 +102,8 @@ for i in range(len(contours)):
# Draw the background marker
cv.circle(markers, (5,5), 3, (255,255,255), -1)
cv.imshow('Markers', markers*10000)
markers_8u = (markers * 10).astype('uint8')
cv.imshow('Markers', markers_8u)
## [seeds]
## [watershed]
@@ -30,7 +30,7 @@ def goodFeaturesToTrack_Demo(val):
print('** Number of corners detected:', corners.shape[0])
radius = 4
for i in range(corners.shape[0]):
cv.circle(copy, (corners[i,0,0], corners[i,0,1]), radius, (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)), cv.FILLED)
cv.circle(copy, (int(corners[i,0,0]), int(corners[i,0,1])), radius, (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)), cv.FILLED)
# Show what you got
cv.namedWindow(source_window)
@@ -30,7 +30,7 @@ def goodFeaturesToTrack_Demo(val):
print('** Number of corners detected:', corners.shape[0])
radius = 4
for i in range(corners.shape[0]):
cv.circle(copy, (corners[i,0,0], corners[i,0,1]), radius, (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)), cv.FILLED)
cv.circle(copy, (int(corners[i,0,0]), int(corners[i,0,1])), radius, (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)), cv.FILLED)
# Show what you got
cv.namedWindow(source_window)
@@ -0,0 +1,41 @@
import numpy as np
from ..accuracy_eval import SemSegmEvaluation
from ..utils import plot_acc
def test_segm_models(models_list, data_fetcher, eval_params, experiment_name, is_print_eval_params=True,
is_plot_acc=True):
if is_print_eval_params:
print(
"===== Running evaluation of the classification models with the following params:\n"
"\t0. val data location: {}\n"
"\t1. val data labels: {}\n"
"\t2. frame size: {}\n"
"\t3. batch size: {}\n"
"\t4. transform to RGB: {}\n"
"\t5. log file location: {}\n".format(
eval_params.imgs_segm_dir,
eval_params.img_cls_file,
eval_params.frame_size,
eval_params.batch_size,
eval_params.bgr_to_rgb,
eval_params.log
)
)
accuracy_evaluator = SemSegmEvaluation(eval_params.log, eval_params.img_cls_file, eval_params.batch_size)
accuracy_evaluator.process(models_list, data_fetcher)
accuracy_array = np.array(accuracy_evaluator.general_fw_accuracy)
print(
"===== End of processing. Accuracy results:\n"
"\t1. max accuracy (top-5) for the original model: {}\n"
"\t2. max accuracy (top-5) for the DNN model: {}\n".format(
max(accuracy_array[:, 0]),
max(accuracy_array[:, 1]),
)
)
if is_plot_acc:
plot_acc(accuracy_array, experiment_name)
@@ -0,0 +1,59 @@
from torchvision import models
from ..pytorch_model import (
PyTorchModelPreparer,
PyTorchModelProcessor,
PyTorchDnnModelProcessor
)
from ...common.utils import set_pytorch_env, create_parser
class PyTorchFcnResNet50(PyTorchModelPreparer):
def __init__(self, model_name, original_model):
super(PyTorchFcnResNet50, self).__init__(model_name, original_model)
def main():
parser = create_parser()
cmd_args = parser.parse_args()
set_pytorch_env()
# Test the base process of model retrieval
resnets = PyTorchFcnResNet50(
model_name="resnet50",
original_model=models.segmentation.fcn_resnet50(pretrained=True)
)
model_dict = resnets.get_prepared_models()
if cmd_args.is_evaluate:
from ...common.test_config import TestConfig
from ...common.accuracy_eval import PASCALDataFetch
from ...common.test.voc_segm_test import test_segm_models
eval_params = TestConfig()
model_names = list(model_dict.keys())
original_model_name = model_names[0]
dnn_model_name = model_names[1]
#img_dir, segm_dir, names_file, segm_cls_colors_file, preproc)
data_fetcher = PASCALDataFetch(
imgs_dir=eval_params.imgs_segm_dir,
frame_size=eval_params.frame_size,
bgr_to_rgb=eval_params.bgr_to_rgb,
)
test_segm_models(
[
PyTorchModelProcessor(model_dict[original_model_name], original_model_name),
PyTorchDnnModelProcessor(model_dict[dnn_model_name], dnn_model_name)
],
data_fetcher,
eval_params,
original_model_name
)
if __name__ == "__main__":
main()
@@ -53,7 +53,7 @@ thickness = 2
sv = svm.getUncompressedSupportVectors()
for i in range(sv.shape[0]):
cv.circle(image, (sv[i,0], sv[i,1]), 6, (128, 128, 128), thickness)
cv.circle(image, (int(sv[i,0]), int(sv[i,1])), 6, (128, 128, 128), thickness)
## [show_vectors]
cv.imwrite('result.png', image) # save the image
@@ -94,13 +94,13 @@ thick = -1
for i in range(NTRAINING_SAMPLES):
px = trainData[i,0]
py = trainData[i,1]
cv.circle(I, (px, py), 3, (0, 255, 0), thick)
cv.circle(I, (int(px), int(py)), 3, (0, 255, 0), thick)
# Class 2
for i in range(NTRAINING_SAMPLES, 2*NTRAINING_SAMPLES):
px = trainData[i,0]
py = trainData[i,1]
cv.circle(I, (px, py), 3, (255, 0, 0), thick)
cv.circle(I, (int(px), int(py)), 3, (255, 0, 0), thick)
## [show_data]
#------------------------- 6. Show support vectors --------------------------------------------
@@ -109,7 +109,7 @@ thick = 2
sv = svm.getUncompressedSupportVectors()
for i in range(sv.shape[0]):
cv.circle(I, (sv[i,0], sv[i,1]), 6, (128, 128, 128), thick)
cv.circle(I, (int(sv[i,0]), int(sv[i,1])), 6, (128, 128, 128), thick)
## [show_vectors]
cv.imwrite('result.png', I) # save the Image
@@ -33,7 +33,7 @@ def hog(img):
return hist
## [hog]
img = cv.imread('digits.png',0)
img = cv.imread(cv.samples.findFile('digits.png'),0)
if img is None:
raise Exception("we need the digits.png image from samples/data here !")
@@ -18,7 +18,7 @@ else:
## [capture]
capture = cv.VideoCapture(cv.samples.findFileOrKeep(args.input))
if not capture.isOpened:
if not capture.isOpened():
print('Unable to open: ' + args.input)
exit(0)
## [capture]
@@ -40,15 +40,16 @@ while(1):
p1, st, err = cv.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
# Select good points
good_new = p1[st==1]
good_old = p0[st==1]
if p1 is not None:
good_new = p1[st==1]
good_old = p0[st==1]
# draw the tracks
for i,(new,old) in enumerate(zip(good_new, good_old)):
a,b = new.ravel()
c,d = old.ravel()
mask = cv.line(mask, (a,b),(c,d), color[i].tolist(), 2)
frame = cv.circle(frame,(a,b),5,color[i].tolist(),-1)
mask = cv.line(mask, (int(a),int(b)),(int(c),int(d)), color[i].tolist(), 2)
frame = cv.circle(frame,(int(a),int(b)),5,color[i].tolist(),-1)
img = cv.add(frame,mask)
cv.imshow('frame',img)
@@ -86,8 +86,8 @@ def main():
framenum = -1 # Frame counter
captRefrnc = cv.VideoCapture(sourceReference)
captUndTst = cv.VideoCapture(sourceCompareWith)
captRefrnc = cv.VideoCapture(cv.samples.findFileOrKeep(sourceReference))
captUndTst = cv.VideoCapture(cv.samples.findFileOrKeep(sourceCompareWith))
if not captRefrnc.isOpened():
print("Could not open the reference " + sourceReference)