用PyCharm运行代码,若未安装可去官网下载社区版
教程如下:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy
可以选择在PyCharm的Terminal中输入这段代码,也可以在cmd中输入python后再输入这段代码
import cv2
import numpy as np
def readVideo(path):
videoCapture = cv2.VideoCapture()
videoCapture.open(path)
frames = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
fps = videoCapture.get(cv2.CAP_PROP_FPS)
for i in range(int (0),int(fps*10)):
ret,frame = videoCapture.read()
#ret是bool类型,frame是image ndarray类型
src = cv2.resize(frame,(int (frame.shape[1]*0.6),int (frame.shape[0]*0.6)))
src = imageToChar(src)
cv2.imshow('frame',src)
cv2.waitKey(1)
def imageToChar(img):
string = "&*@#%qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
count = len(string)
u, v, _= img.shape
c = img*0 + 0
# c = np.zeros((u,v))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imshow('output', c)
for i in range(0, u-1,10):
for j in range(0, v-1,10):
pix = gray[i, j]
b, g, r = img[i, j]
zifu = string[int(((count - 1) * pix) / 256)]
cv2.putText(c, zifu, (j, i), cv2.FONT_HERSHEY_COMPLEX, 0.5,(int(255),int(255),int(255)))
return c
readVideo('image/test.mp4')
cv2.waitKey(0)
在安装PyCharm的目录下新建一个名为image的文件夹,再在这个文件夹下面放入视频,视频名字命名为test.mp4,若运行失败,则将倒数第二行的代码readVideo('image/test.mp4)改为readVideo('../image/test.mp4)
蔡徐坤唱跳Rap 4K高清视频地址:
4K 蔡徐坤原版
(可以用手机端进入链接下载视频)
唱跳Rap