forked from yaochao/img_to_txt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_to_txt.py
executable file
·84 lines (67 loc) · 2.07 KB
/
video_to_txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# created by yaochao at 2019/3/28
# https://www.geeksforgeeks.org/extract-images-from-video-in-python/
import cv2
from PIL import Image
import numpy as np
import time
import sys
import os
abc = '@MWNHB8$06XFVYZ27>1jli!;:,. '
l = 256 / len(abc)
def remove_transparency(im, bg_colour=(255, 255, 255)):
if im.mode in ('RGBA', 'LA') or (im.mode == 'P' and 'transparency' in im.info):
alpha = im.convert('RGBA').split()[-1]
bg = Image.new("RGBA", im.size, bg_colour + (255,))
bg.paste(im, mask=alpha)
return bg
else:
return im
def img2pixel(img, charwidth=100):
img = img.convert("L")
w, h = img.size
img = img.resize((charwidth, int(charwidth * (h / w) / 2.4)))
data = np.array(img)
return data
def pixel2char(data):
chars = '\n\n'
for row in data:
for pixel in row:
a = abc[int(pixel / l)]
chars += a
chars += '\n'
return chars + '\n'
def main(args):
start = time.time()
if len(args) != 3:
print("Usage: video_to_txt videopath charwidth")
return
# 获取视频的宽高和帧率信息
vcap = cv2.VideoCapture(args[1])
width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH) # float
height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float
fps = vcap.get(cv2.CAP_PROP_FPS)
print(width, height, fps)
# 开始按帧循环
currentframe = 0
while (True):
t1 = time.time()
ret, frame = vcap.read()
if ret:
pilimg = Image.fromarray(frame)
data = img2pixel(pilimg, charwidth=int(args[2]))
s = pixel2char(data=data)
print(s)
currentframe += 1
else:
break
# 睡眠时间为fps分之一秒减去此帧已经使用过的时间
time2sleep = (1 / fps) - (time.time() - t1)
if time2sleep > 0: time.sleep(time2sleep)
# 释放空间和窗口
vcap.release()
cv2.destroyAllWindows()
print("costed time:", time.time() - start)
if __name__ == '__main__':
main(sys.argv)