Python is super simple to realize the beating heart code/opencv/dozens of lines of code/novice can also learn

Python is super simple to realize the beating heart code/opencv/dozens of lines of code/novice can also learn

insert image description here

1. OpenCV processes video frame by frame to obtain coordinate pixel points

Look for this same love video on the Internet. The source of the original video is "Ignite Me, Warm You", and intercept the beating of a cycle of love. If you don't know how to edit software, you can also use opencv to edit. The code reference is as follows: opencv capture video The heartbeat video of the code
can also be excerpted from some up master clips from B.
After intercepting the video clip, opencv processes the video frame by frame, mainly to eliminate noise and interference information in the video, and only keep the pixel information of the heart information.

If you don’t want to do the above steps to intercept and process the video, if you want to get the pixel information directly, you can also leave your email and I will send it to you

import numpy as np
import cv2 as cv
dict = {
    
    } # 空字典用来储存每一帧的像素点信息

# 检查视频是否成功读取
def cv_isOpen(vc):
    if vc.isOpened():
        # open是bool类型,提示视频是否打开
        # frame是当前帧的数据
        open, frame = vc.read()
        return open, frame
    else:
        open = False
        return open
        
# 将当前帧爱心信息的像素点保存为列表待用
def cv_threshold(img, lower: int, upper: int):
   lst = []
   for i in range(0, len(img)):
      for j in range(0, len(img[i])):
         if img[i][j][2] > lower and img[i][j][2] < upper:
           x = j
           y = i
           lst.append((x, y, random.randint(1,2)))
   return lst


def cv_getPixel(vc):
    open = cv_isOpen(vc)
    i = 0
    while open:
        _open, frame = vc.read()# 一帧一帧读取
        if frame is None:
        	print('读取失败')
            break
        if _open:
           # 视频帧截取成700*700,根据爱心位置设置截取位置
           area = frame[300:1000, 0:700]
           # 根据爱心相关信息像素点位置保存,像素点信息集中位置的阈值不同视频下有差异
           dict[i] = cv_threshold(area, 70, 170)
           i+=1
 
if __name__ == '__main__':
	vc = cv.VideoCapture('heart.mp4')
	cv_getPixel(vc)
	# 储存到npy文件中
	np.save('heartPixel.npy', dict)

2. Use tkinter to reproduce the information of each frame

import numpy as np
from tkinter import *

# 两个偏置值调整爱心在画布的位置
X_BIAS = 250 
Y_BIAS = 0
FRAMES_SKIP = 5 # 每次跳的帧数,越大越快,越小越丝滑,根据计算机性能设置
TOTAL_FRAMES = 50 # 我截取的一个跳动周期有50帧的信息
COLOR = '#ff7171' # 我选取这个粉红色

def draw(main: Tk, render_canvas: Canvas, render_frame=0):
    render_canvas.delete('all')
    render(render_canvas, render_frame)
    main.after(1, draw, main, render_canvas, render_frame + FRAMES_SKIP)

def render(render_canvas: Canvas, render_frame):
    for x, y, size in dict[render_frame % TOTAL_FRAMES]:
        render_canvas.create_rectangle(x+X_BIAS, y+Y_BIAS, size+x+X_BIAS, size+y+Y_BIAS, width=0, fill=COLOR)

if __name__ == '__main__':
    dict = np.load('heartPixel.npy', allow_pickle='TRUE').item()
    root = Tk()
    canvas = Canvas(root, bg='black', height=720, width=1280)
    canvas.pack()
    draw(root, canvas)
    root.mainloop()

3. Supplement

If too many pixels are generated, resulting in very slow operation, you can use the following function to reduce the pixel information, and you can use it several times without affecting the result.

import random
import numpy as np
dict = np.load('heartPixel.npy', allow_pickle='TRUE').item()
for i in range(TOTAL_FRAMES):
    j = 0
    while j < len(dict[i]):
        del new_dict[i][j]
        j+=2
np.save('newHeartPixel.npy', dict)

Guess you like

Origin blog.csdn.net/Dec1steee/article/details/127832276