Permalink
Browse files

fix bug when processing a video image by image

  • Loading branch information...
sdpython committed Jan 20, 2018
1 parent 58ede2d commit 065cbe9129f22f4b36c6c2f064d056cf0ae8f384
@@ -48,7 +48,7 @@
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from skimage.io._plugins.pil_plugin import pil_to_ndarray
from PIL import Image
from src.code_beatrix.art.video_drawing import rectangle
from src.code_beatrix.art.video_drawing import rectangle, blur
class TestVideoDrawing(ExtTestCase):
@@ -71,6 +71,24 @@ def test_rectangle(self):
dest = os.path.join(temp, "img_rect.jpg")
pil_img2.save(dest)
def test_blur(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_blur")
img = os.path.join(temp, '..', 'data', 'img1.jpg')
pil_img = Image.open(img)
skimg = pil_to_ndarray(pil_img)
im1 = skimg.copy().ravel()
blur(skimg, (10, 20), (100, 200))
im2 = skimg.copy().ravel()
self.assertTrue(im1.tolist() != im2.tolist())
pil_img2 = Image.fromarray(skimg)
dest = os.path.join(temp, "img_blur.jpg")
pil_img2.save(dest)
if __name__ == "__main__":
unittest.main()
@@ -47,7 +47,7 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.videodl import video_map_images
from src.code_beatrix.art.video import video_save, video_extract_video, video_save_image
from src.code_beatrix.art.video import video_save, video_extract_video, video_save_image, clean_video
class TestVideoDLFace(ExtTestCase):
@@ -58,14 +58,14 @@ def test_modify_avideo_blur(self):
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_videodl_face_blur")
vid = os.path.join(temp, '..', 'data', 'mary.mp4')
vid = os.path.join(temp, '..', 'data', 'charlie.mp4')
vide = video_extract_video(vid, 0, 5 if __name__ == "__main__" else 1)
vid2 = video_map_images(
vide, fps=10, name="detect", progress_bar=__name__ == "__main__", fLOG=fLOG)
exp = os.path.join(temp, "face.mp4")
video_save(vid2, exp)
self.assertExists(exp)
vid2.cbclean()
clean_video(vid2)
def test_modify_avideo(self):
fLOG(
@@ -74,7 +74,7 @@ def test_modify_avideo(self):
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_videodl_face_rect")
vid = os.path.join(temp, '..', 'data', 'charlie.mp4')
vide = video_extract_video(vid, 0, 5 if __name__ == "__main__" else 1)
vide = video_extract_video(vid, 0, 10 if __name__ == "__main__" else 1)
vid2 = video_map_images(
vide, fps=10, name="detect", action="rect", progress_bar=__name__ == "__main__", fLOG=fLOG)
exp = os.path.join(temp, "face.mp4")
@@ -84,7 +84,7 @@ def test_modify_avideo(self):
im.save(os.path.join(temp, "img2.png"))
video_save(vid2, exp, verbose=__name__ == "__main__")
self.assertExists(exp)
# vid2.cbclean()
clean_video(vid2)
if __name__ == "__main__":
@@ -47,7 +47,7 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.videodl import video_map_images
from src.code_beatrix.art.video import video_save, video_extract_video
from src.code_beatrix.art.video import video_save, video_extract_video, clean_video
class TestVideoDLPeople(ExtTestCase):
@@ -65,7 +65,7 @@ def test_modify_avideo(self):
exp = os.path.join(temp, "people.mp4")
video_save(vid2, exp)
self.assertExists(exp)
vid2.cbclean()
clean_video(vid2)
if __name__ == "__main__":
@@ -2,20 +2,20 @@
@file
@brief Draws objects on videos.
"""
import numpy
from cv2 import blur as cv_blur, rectangle as cv_rectangle
from cv2 import LINE_AA
def blur(img, p1, p2, kernel_size=(5, 5)):
def blur(img, p1, p2, frac=0.333, kernel_size=None):
"""
Blurs a part of a picture.
Uses `blur <https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=blur#blur>`_.
@param img image (:epkg:`numpy:array`)
@param p1 (x1,y1)
@param p2 (x2, y2)
@param kernel_size kernel size for the bluring.
@param frac if not None, if *kernel_size* is equal to this fraction
of the original frame
@param kernel_size kernel size for the bluring (wins over *frac*)
It modifies the original picture.
"""
@@ -26,14 +26,13 @@ def blur(img, p1, p2, kernel_size=(5, 5)):
y1, y2 = max(0, y1), min(y2, h)
dx, dy = x2 - x1, y2 - y1
region_size = dy, dx
r_blur = max(min(region_size) * 2 // 3, 5)
mask = numpy.zeros(region_size).astype('uint8')
cv_rectangle(mask, (y1, x1), (y2, x2), 255, -1, lineType=LINE_AA)
mask = numpy.dstack(3 * [(1.0 / 255) * mask])
if kernel_size is not None:
blur_size = kernel_size
else:
blur_size = (int(frac * dx), int(frac * dy))
orig = img[y1:y2, x1:x2]
zone = cv_blur(orig, (r_blur, r_blur))
img[y1:y2, x1:x2] = mask * zone + (1 - mask) * orig
zone = cv_blur(orig, blur_size)
img[y1:y2, x1:x2] = zone
def rectangle(img, p1, p2, color=(255, 255, 0)):
@@ -291,4 +291,4 @@ def fl_rect(gf, t, rects):
non, len(all_rects)))
with VideoContext(video_or_file) as video:
return video.video.fl(lambda im, t: fcts[action](im, t, rects), keep_duration=True)
return video.video.fl(lambda im, t: fcts[action](im, t, all_rects), keep_duration=True)

0 comments on commit 065cbe9

Please sign in to comment.