annotate scripts/undistort-video.py @ 1240:bb14f919d1cb

cleaned use of centile (np only) and added info in classify-objects
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 05 Feb 2024 14:14:14 -0500
parents b684135d817f
children 2aa56b101041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 950
diff changeset
1 #! /usr/bin/env python3
572
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 import sys, argparse
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
5 import numpy as np
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 import cv2
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7
1090
97247e44b827 module import bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1024
diff changeset
8 from trafficintelligence import cvutils
572
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9 from math import ceil, log10
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
10 from pathlib import Path
572
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
11
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
12 parser = argparse.ArgumentParser(description='''The program converts a video into a series of images corrected for distortion. One can then use mencoder to generate a movie, eg
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
13 $ mencoder 'mf://./*.png' -mf fps=[framerate]:type=png -ovc xvid -xvidencopts bitrate=[bitrate] -nosound -o [output.avi]''')
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
14
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
15 parser.add_argument('-i', dest = 'videoFilename', help = 'filename of the video sequence')
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
16 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file')
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
17 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float)
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
18 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float, default = 1.)
1238
b684135d817f version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1090
diff changeset
19 parser.add_argument('-k', dest = 'maskFilename', help = 'name of the mask file, to undistort to see how it covers the undistortion errors')
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 770
diff changeset
20 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int, default = 0)
572
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
21 parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save', type = int)
760
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
22 parser.add_argument('-d', dest = 'destinationDirname', help = 'name of the directory where the undistorted frames are saved')
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
23 parser.add_argument('--encode', dest = 'encodeVideo', help = 'indicate if video is generated at the end (default Xvid)', action = 'store_true')
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
24 parser.add_argument('--fps', dest = 'fps', help = 'frame per second of the output video file if encoding', type = float, default = 30)
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
25 parser.add_argument('--bitrate', dest = 'bitrate', help = 'bitrate of the output video file if encoding', type = int, default = 5000)
572
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27 args = parser.parse_args()
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29 intrinsicCameraMatrix = np.loadtxt(args.intrinsicCameraMatrixFilename)
760
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
30 if args.destinationDirname is None:
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
31 destinationPath = Path('.')
760
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
32 else:
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
33 destinationPath = Path(destinationPath)
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
34 if not destinationPath.exists():
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
35 destinationPath.mkdir()
572
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
36
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
37 capture = cv2.VideoCapture(args.videoFilename)
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
38 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
39 height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
950
c03d2c0a4c04 corrected bugs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 926
diff changeset
40 [map1, map2], newCameraMatrix = cvutils.computeUndistortMaps(width, height, args.undistortedImageMultiplication, intrinsicCameraMatrix, args.distortionCoefficients)
926
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
41 if args.maskFilename is not None:
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
42 mask = cv2.imread(args.maskFilename)
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
43 undistortedMask = cv2.remap(mask, map1, map2, interpolation=cv2.INTER_LINEAR)/255
924
a71455bd8367 work in progress on undistortion acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
44
572
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
45 if capture.isOpened():
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
46 ret = True
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
47 frameNum = args.firstFrameNum
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
48 capture.set(cv2.CAP_PROP_POS_FRAMES, args.firstFrameNum)
636
3058e00887bc removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 572
diff changeset
49 if args.lastFrameNum is None:
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 950
diff changeset
50 lastFrameNum = float('inf')
572
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
51 else:
9c429c7efe89 added script to generate undistorted images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
52 lastFrameNum = args.lastFrameNum
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 770
diff changeset
53 nZerosFilename = int(ceil(log10(lastFrameNum)))
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 770
diff changeset
54 while ret and frameNum < lastFrameNum:
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 770
diff changeset
55 ret, img = capture.read()
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 770
diff changeset
56 if ret:
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 770
diff changeset
57 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
58 cv2.imwrite(str(destinationPath/Path('undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum))), img)
926
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
59 if args.maskFilename is not None:
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
60 cv2.imwrite(str(destinationPath/Path('undistorted+mask-{{:0{}}}.png'.format(nZerosFilename).format(frameNum))), cv2.multiply(img, undistortedMask, dtype = 16))
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 770
diff changeset
61 frameNum += 1
760
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
62
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
63 if args.encodeVideo:
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
64 print('Encoding the images files in video')
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
65 from subprocess import check_call
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
66 from storage import openCheck
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
67 out = openCheck("err.log", "w")
1024
acb4f6f6545d corrected issues with update to OpenCV 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
68 check_call("mencoder \'mf://"+destinationPath+"*.png\' -mf fps={}:type=png -ovc xvid -xvidencopts bitrate={} -nosound -o ".format(args.fps, args.bitrate)+destinationDirname+"undistort.avi", stderr = out, shell = True)
760
d72e4bcc1e36 added functionality to generate avi from the undistorted videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 636
diff changeset
69 out.close()