Opencv, 给视频加一个进度条 - 图1
© Karobben

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Opencv Progress Bar

Prepare your video, gif, png

Sample progress bar

We can use a rectangle to be the progress bar

  1. import cv2 as cv2
  2. # Vdieo source
  3. Video = "/run/media/ken/Data/Vlog/Tank_rasbbery/test.avi"
  4. cap=cv2.VideoCapture(Video)
  5. Video_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
  6. frame_total = cap.get(cv2.CAP_PROP_FRAME_COUNT)
  7. Num = 0
  8. print(frame_total, Num)
  9. while Num <= frame_total-1:
  10. # caculate the current frame
  11. # The width of the progress bar = (current frame /Total frame) * Width of the frame
  12. Width = int((( Num/frame_total)*420))
  13. Num +=1
  14. print(Num, frame_total, width)
  15. # features for rectangle
  16. ptLeftTop = (0, 0)
  17. ptRightBottom = (Width, 20)
  18. point_color = (0, 0, 255) # BGR
  19. thickness = 20
  20. lineType = 8
  21. # frame
  22. ret,frame=cap.read()
  23. frame = cv2.resize(frame, (420,360), interpolation = cv2.INTER_AREA)
  24. frame = cv2.rectangle(frame, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
  25. cv2.imshow("video",frame)
  26. # 在播放每一帧时,使用cv2.waitKey()设置适当的持续时间。如果设置的太低视频就会播放的非常快,如果设置的太高就会播放的很慢。通常情况下25ms就ok
  27. if cv2.waitKey(25)&0xFF==ord('q'):
  28. cv2.destroyAllWindows()
  29. break
  30. print("Mission Down")

loading a picture and moving with progress bar

First, loading the picture

  1. Picture = "/home/ken/Pictures/pokeball.png"
  2. img = cv2.imread(Picture,1)
  3. img = cv2.resize(img, (10,10), interpolation = cv2.INTER_AREA)

Then, picture clean:
Details about threads and mask: 假小牙 2017

Origing by: 万能的小黑Alex 2020
Transparent Background

  1. def CV_mask(img1, img2, rows1, rows2, cols1, cols2):
  2. # 把logo放在左上角,所以我们只关心这一块区域
  3. roi = img1[rows1:rows2, cols1:cols2]
  4. # 创建掩膜
  5. img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  6. print(img2gray[0])
  7. ret, mask = cv2.threshold(img2gray, 56, 255, cv2.THRESH_BINARY)
  8. mask_inv = cv2.bitwise_not(mask)
  9. # 保留除logo外的背景
  10. img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
  11. dst = cv2.add(img1_bg, img2) # 进行融合
  12. img1[rows1:rows2, cols1:cols2] = dst # 融合后放在原图上
  13. return img1

or

Origing by: 红薯爱帅 2017
Clear Background

  1. def img_deal(img_target, img_logo, row1, row2, col1, col2):
  2. # 1,对logo进行缩放,按照20%进行
  3. # cv2.imshow("img_logo", img_logo)
  4. # 2,对logo做清洗,白色区域是255,其他区域置为黑色0
  5. img_logo_gray = cv2.cvtColor(img_logo, cv2.COLOR_BGR2GRAY)
  6. print(img_logo_gray[0])
  7. ret, img_logo_mask = cv2.threshold(img_logo_gray, 46, 255, cv2.THRESH_BINARY) # 二值化函数
  8. img_logo_mask1 = cv2.bitwise_not(img_logo_mask)
  9. # cv2.imshow("img_logo_gray", img_logo_gray)
  10. # cv2.imshow("img_logo_mask", img_logo_mask)
  11. # 3,提取目标图片的ROI
  12. img_roi = img_target[col1:col2, row1:row2].copy()
  13. #cv2.imshow("img_roi", img_roi)
  14. # 4,ROI和Logo图像融合
  15. img_res0 = cv2.bitwise_and(img_roi, img_roi, mask=img_logo_mask)
  16. img_res1 = cv2.bitwise_and(img_logo, img_logo, mask=img_logo_mask1)
  17. img_res2 = cv2.add(img_res0, img_res1)
  18. # img_res2 = img_res0 + img_res1
  19. img_target[col1:col2, row1:row2] = img_res2[:, :]
  20. return img_target

Finally, adding picture to frames

  1. import cv2 as cv2
  2. import numpy as np
  3. # Vdieo source
  4. Video = "/run/media/ken/Data/Vlog/Tank_rasbbery/test.avi"
  5. Picture = "/home/ken/Pictures/16048269.png"
  6. # OUTPUT size of the video
  7. Width_out = int(1920*.9)
  8. Height_out = int(1080*.9)
  9. PB_h_r = 1- 0.5
  10. # Resize image
  11. PB_img_wr = .025
  12. img = cv2.imread(Picture,1)
  13. #img = cv2.resize(img, (200, 300), interpolation = cv2.INTER_AREA)
  14. # progress bar
  15. point_color = (0, 0, 255) # BGR
  16. lineType = 8
  17. thickness_r = 0.005
  18. cap=cv2.VideoCapture(Video)
  19. # config
  20. PB_H = int(Height_out*PB_h_r)
  21. Video_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
  22. frame_total = cap.get(cv2.CAP_PROP_FRAME_COUNT)
  23. Num = 0
  24. ## img_position in frame
  25. PB_img_hr = (img.shape[0]/ img.shape[1] )* PB_img_wr
  26. PB_img_w = int(PB_img_wr * Height_out)
  27. PB_img_h = int(PB_img_hr * Height_out)
  28. if PB_img_h%2 == 1:
  29. PB_img_h += 1
  30. frame_img_h1 = PB_H -int(PB_img_h/2)
  31. frame_img_h2 = PB_H +int(PB_img_h/2)
  32. img = cv2.resize(img, (PB_img_w,PB_img_h), interpolation = cv2.INTER_AREA)
  33. ## Thickness of Progress bar
  34. thickness = int(thickness_r * Height_out)
  35. def img_deal(img_target, img_logo, row1, row2, col1, col2):
  36. # 1,对logo进行缩放,按照20%进行
  37. # cv2.imshow("img_logo", img_logo)
  38. # 2,对logo做清洗,白色区域是255,其他区域置为黑色0
  39. img_logo_gray = cv2.cvtColor(img_logo, cv2.COLOR_BGR2GRAY)
  40. print(img_logo_gray[0])
  41. ret, img_logo_mask = cv2.threshold(img_logo_gray, 46, 255, cv2.THRESH_BINARY) # 二值化函数
  42. img_logo_mask1 = cv2.bitwise_not(img_logo_mask)
  43. # cv2.imshow("img_logo_gray", img_logo_gray)
  44. # cv2.imshow("img_logo_mask", img_logo_mask)
  45. # 3,提取目标图片的ROI
  46. img_roi = img_target[col1:col2, row1:row2].copy()
  47. #cv2.imshow("img_roi", img_roi)
  48. # 4,ROI和Logo图像融合
  49. img_res0 = cv2.bitwise_and(img_roi, img_roi, mask=img_logo_mask)
  50. img_res1 = cv2.bitwise_and(img_logo, img_logo, mask=img_logo_mask1)
  51. img_res2 = cv2.add(img_res0, img_res1)
  52. # img_res2 = img_res0 + img_res1
  53. img_target[col1:col2, row1:row2] = img_res2[:, :]
  54. return img_target
  55. def CV_mask(img1, img2, rows1, rows2, cols1, cols2):
  56. # 把logo放在左上角,所以我们只关心这一块区域
  57. roi = img1[rows1:rows2, cols1:cols2]
  58. # 创建掩膜
  59. img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  60. print(img2gray[0])
  61. ret, mask = cv2.threshold(img2gray, 46, 255, cv2.THRESH_BINARY_INV)
  62. mask_inv = cv2.bitwise_not(mask)
  63. # 保留除logo外的背景
  64. img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
  65. dst = cv2.add(img1_bg, img2) # 进行融合
  66. img1[rows1:rows2, cols1:cols2] = dst # 融合后放在原图上
  67. return img1
  68. while Num <= frame_total-1:
  69. # caculate the current frame
  70. # The width of the progress bar = (current frame /Total frame) * Width of the frame
  71. Width = int((( Num/frame_total)*Width_out))
  72. Num +=1
  73. # features for rectangle
  74. ptLeftTop = (0, PB_H)
  75. print(Num, frame_total, Width, PB_H, print(img[0][0]))
  76. ptRightBottom = (Width, PB_H)
  77. # frame
  78. ret,frame=cap.read()
  79. frame = cv2.resize(frame, (Width_out,Height_out), interpolation = cv2.INTER_AREA)
  80. frame = cv2.rectangle(frame, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
  81. # Adding png for progress bar
  82. Width_png = int((( Num/frame_total)*(Width_out-PB_img_w)))
  83. # 保留除logo外的背景
  84. #frame =
  85. CV_mask(frame, img, frame_img_h1, frame_img_h2, 0+Width_png, PB_img_w+Width_png)
  86. #frame = img_deal(frame, img, 0+Width_png, PB_img_w+Width_png, frame_img_h1, frame_img_h2)
  87. #frame[frame_img_h1:frame_img_h2 ,0+Width_png:PB_img_w+Width_png] = img
  88. # frame show
  89. cv2.imshow("video",frame)
  90. # 在播放每一帧时,使用cv2.waitKey()设置适当的持续时间。如果设置的太低视频就会播放的非常快,如果设置的太高就会播放的很慢。通常情况下25ms就ok
  91. if cv2.waitKey(25)&0xFF==ord('q'):
  92. cv2.destroyAllWindows()
  93. break
  94. print("Mission Down")

Gif progress

Loading gif

  1. import time, cv2
  2. from PIL import Image
  3. import numpy as np
  4. file = "/home/ken/Pictures/pikachu_run.gif"
  5. List = []
  6. im = Image.open(file)
  7. im.seek(1)#skip to the second frame
  8. try:
  9. while 1:
  10. List += [cv2.cvtColor(np.asarray(im.convert()),cv2.COLOR_RGB2BGR)]
  11. im.seek(im.tell()+1)
  12. except EOFError:#the sequence ends
  13. pass
  14. Num = 0
  15. while Num < len(List)*2:
  16. Num +=1
  17. ID = Num%(len(List))
  18. cv2.imshow("OpenCV",List[ID])
  19. print(ID)
  20. cv2.waitKey(1)
  21. time.sleep(0.1)
  22. cv2.destroyAllWindows()

All codes

  1. import cv2 as cv2
  2. import numpy as np
  3. from PIL import Image
  4. # Vdieo source
  5. Video = "/run/media/ken/Data/Vlog/Tank_rasbbery/test.avi"
  6. Picture = "/home/ken/Pictures/test.gif"
  7. # OUTPUT size of the video
  8. Width_out = int(1920*.9)
  9. Height_out = int(1080*.9)
  10. PB_h_r = 1- 0.05
  11. # Resize image
  12. PB_img_wr = .1
  13. img = cv2.imread(Picture,1)
  14. #img = cv2.resize(img, (200, 300), interpolation = cv2.INTER_AREA)
  15. # progress bar
  16. point_color = (0, 0, 255) # BGR
  17. lineType = 8
  18. thickness_r = 0.005
  19. cap=cv2.VideoCapture(Video)
  20. # config
  21. PB_H = int(Height_out*PB_h_r)
  22. Video_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
  23. frame_total = cap.get(cv2.CAP_PROP_FRAME_COUNT)
  24. Num = 0
  25. ## img_position in frame
  26. #PB_img_hr = (img.shape[0]/ img.shape[1] )* PB_img_wr
  27. #PB_img_w = int(PB_img_wr * Height_out)
  28. #PB_img_h = int(PB_img_hr * Height_out)
  29. #if PB_img_h%2 == 1:
  30. # PB_img_h += 1
  31. #frame_img_h1 = PB_H -int(PB_img_h/2)
  32. #frame_img_h2 = PB_H +int(PB_img_h/2)
  33. #img = cv2.resize(img, (PB_img_w,PB_img_h), interpolation = cv2.INTER_AREA)
  34. ## Thickness of Progress bar
  35. thickness = int(thickness_r * Height_out)
  36. def img_deal(img_target, img_logo, row1, row2, col1, col2, THRE):
  37. # 1,对logo进行缩放,按照20%进行
  38. # cv2.imshow("img_logo", img_logo)
  39. # 2,对logo做清洗,白色区域是255,其他区域置为黑色0
  40. img_logo_gray = cv2.cvtColor(img_logo, cv2.COLOR_BGR2GRAY)
  41. print(img_logo_gray[0])
  42. ret, img_logo_mask = cv2.threshold(img_logo_gray, THRE, 255, cv2.THRESH_BINARY) # 二值化函数
  43. img_logo_mask1 = cv2.bitwise_not(img_logo_mask)
  44. # cv2.imshow("img_logo_gray", img_logo_gray)
  45. # cv2.imshow("img_logo_mask", img_logo_mask)
  46. # 3,提取目标图片的ROI
  47. img_roi = img_target[col1:col2, row1:row2].copy()
  48. #cv2.imshow("img_roi", img_roi)
  49. # 4,ROI和Logo图像融合
  50. img_res0 = cv2.bitwise_and(img_roi, img_roi, mask=img_logo_mask)
  51. img_res1 = cv2.bitwise_and(img_logo, img_logo, mask=img_logo_mask1)
  52. img_res2 = cv2.add(img_res0, img_res1)
  53. # img_res2 = img_res0 + img_res1
  54. img_target[col1:col2, row1:row2] = img_res2[:, :]
  55. return img_target
  56. # reading GIF file
  57. List = []
  58. im = Image.open(Picture)
  59. im.seek(1)#skip to the second frame
  60. try:
  61. while 1:
  62. img = cv2.cvtColor(np.asarray(im.convert()),cv2.COLOR_RGB2BGR)
  63. PB_img_hr = (img.shape[0]/ img.shape[1] )* PB_img_wr
  64. PB_img_w = int(PB_img_wr * Height_out)
  65. PB_img_h = int(PB_img_hr * Height_out)
  66. if PB_img_h%2 == 1:
  67. PB_img_h += 1
  68. frame_img_h1 = PB_H -int(PB_img_h/2)
  69. frame_img_h2 = PB_H +int(PB_img_h/2)
  70. List += [cv2.resize(img, (PB_img_w,PB_img_h), interpolation = cv2.INTER_AREA)]
  71. im.seek(im.tell()+1)
  72. except EOFError:#the sequence ends
  73. pass
  74. def CV_mask(img1, img2, rows1, rows2, cols1, cols2, THRE):
  75. # 把logo放在左上角,所以我们只关心这一块区域
  76. roi = img1[rows1:rows2, cols1:cols2]
  77. # 创建掩膜
  78. img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  79. print(img2gray[0])
  80. ret, mask = cv2.threshold(img2gray, THRE, 255, cv2.THRESH_BINARY_INV)
  81. mask_inv = cv2.bitwise_not(mask)
  82. # 保留除logo外的背景
  83. img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
  84. dst = cv2.add(img1_bg, img2) # 进行融合
  85. img1[rows1:rows2, cols1:cols2] = dst # 融合后放在原图上
  86. return img1
  87. while Num <= frame_total-1:
  88. # caculate the current frame
  89. # The width of the progress bar = (current frame /Total frame) * Width of the frame
  90. Width = int((( Num/frame_total)*(Width_out-PB_img_w))) + int(PB_img_w*.5)
  91. Num +=1
  92. # features for rectangle
  93. ptLeftTop = (0, PB_H)
  94. img = List[int(Num%len(List))]
  95. img = cv2.flip(img,1 )
  96. print(Num, frame_total, Width, PB_H, print(img[0][0]))
  97. ptRightBottom = (Width, PB_H)
  98. # frame
  99. ret,frame=cap.read()
  100. frame = cv2.resize(frame, (Width_out,Height_out), interpolation = cv2.INTER_AREA)
  101. frame = cv2.rectangle(frame, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
  102. # Adding png for progress bar
  103. Width_png = int((( Num/frame_total)*(Width_out-PB_img_w)))
  104. # 保留除logo外的背景
  105. #frame =
  106. #CV_mask(frame, img, frame_img_h1, frame_img_h2, 0+Width_png, PB_img_w+Width_png, 207)
  107. frame = img_deal(frame, img, 0+Width_png, PB_img_w+Width_png, frame_img_h1, frame_img_h2, 207)
  108. #frame[frame_img_h1:frame_img_h2 ,0+Width_png:PB_img_w+Width_png] = img
  109. # frame show
  110. cv2.imshow("video",frame)
  111. # 在播放每一帧时,使用cv2.waitKey()设置适当的持续时间。如果设置的太低视频就会播放的非常快,如果设置的太高就会播放的很慢。通常情况下25ms就ok
  112. if cv2.waitKey(25)&0xFF==ord('q'):
  113. cv2.destroyAllWindows()
  114. break
  115. print("Mission Down")

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗