Coverage for src/code_beatrix/art/video_drawing.py: 95%

21 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-04-29 13:45 +0200

1""" 

2@file 

3@brief Draws objects on videos. 

4""" 

5from cv2 import blur as cv_blur, rectangle as cv_rectangle # pylint: disable=E0401 

6 

7 

8def blur(img, p1, p2, frac=0.333, kernel_size=None): 

9 """ 

10 Blurs a part of a picture. 

11 Uses `blur <https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=blur#blur>`_. 

12 

13 @param img image (:epkg:`numpy:array`) 

14 @param p1 (x1,y1) 

15 @param p2 (x2, y2) 

16 @param frac if not None, if *kernel_size* is equal to this fraction 

17 of the original frame 

18 @param kernel_size kernel size for the bluring (wins over *frac*) 

19 

20 It modifies the original picture. 

21 """ 

22 h, w, _ = img.shape 

23 x1, y1 = p1 

24 x2, y2 = p2 

25 x1, x2 = max(0, x1), min(x2, w) 

26 y1, y2 = max(0, y1), min(y2, h) 

27 dx, dy = x2 - x1, y2 - y1 

28 

29 if kernel_size is not None: 

30 blur_size = kernel_size 

31 else: 

32 blur_size = (int(frac * dx), int(frac * dy)) 

33 orig = img[y1:y2, x1:x2] 

34 zone = cv_blur(orig, blur_size) 

35 img[y1:y2, x1:x2] = zone 

36 

37 

38def rectangle(img, p1, p2, color=(255, 255, 0)): 

39 """ 

40 Draws a rectangle. 

41 Uses `blur <https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=blur#blur>`_. 

42 

43 @param img image (:epkg:`numpy:array`) 

44 @param p1 (x1,y1) 

45 @param p2 (x2, y2) 

46 @param kernel_size kernel size for the bluring. 

47 

48 It modifies the original picture. 

49 """ 

50 h, w, _ = img.shape 

51 x1, y1 = p1 

52 x2, y2 = p2 

53 x1, x2 = max(0, x1), min(x2, w) 

54 y1, y2 = max(0, y1), min(y2, h) 

55 cv_rectangle(img, (x1, y1), (x2, y2), color)