opencv python toy project . Image Process 小练习 ,给某人来个恶作剧。
为了方便pyinstaller打包成.exe文件:代码全堆在一个.py文件、导包时用什么导什么、不依赖外部资源文件,硬编码在代码里。但打包后还是出现问题:写文件时失败且文件名中文乱码、sleep()作用后的输出流时间不对。
pyinstaller -F main.py -i icon.ico
# import birthday.toy as toy
from threading import Thread
from time import time
from time import strftime
from time import localtime
from time import sleep
import cv2 as cv
from random import randint
# from numpy import log2
# from numpy import int as npint
# from numpy import power
from numpy import array
import numpy.core._dtype_ctypes
def video(name, duration):
cap = cv.VideoCapture(0, cv.CAP_DSHOW)
fourcc = cv.VideoWriter_fourcc(*'DIVX')
cur_time = strftime('at%Mm%Ss', localtime())
out = cv.VideoWriter('D:/'+name+cur_time+'.avi', fourcc, 25.0, (640, 480), True)
if not cap.isOpened():
cap.open()
start_t = int(time())
frame_cnt = 0
while True:
ret, frame = cap.read()
if not ret:
break
out.write(frame)
frame_cnt += 1
# cv.imshow('video', frame)
# x = cv.waitKey(1) # -1
# if x == ord('q'):
# break
end_t = int(time())
if end_t - start_t >= duration:
cv.imwrite('D:/I_saw_you_'+cur_time+'.png', frame) # when I packing
break
# print('frame count: ', frame_cnt)
cap.release()
out.release()
cv.destroyAllWindows()
def text(duration = 60):
print("Read the following carefully")
cont = '语言和数字的产生目的:记录传播信息。\n\
象形文字中读音相同的词用同一符号记录:概念的聚类。\n\
不同文字系统在记录信息能力上等价,苹果<=>apple,用数字可以存储同样的信息(即现代通信)。\n\
爱(象形文字,物体外表描述) --> love(拼音文字,抽象符号化)是人类的飞跃(我骄傲的方块字呢?)。把笔画作为字母,中文其实是二维的拼音文字。\n\
常用字短,生僻字长:符合最短编码理论。\n\
古代刻字不易-->古文简洁<=>难懂:信道越窄,传递前尽可能压缩,接收端需解码。同时期口语和今白话文差不多(人与人说话(宽信道)不费事)\n\
词:有限且封闭集合(完备集,可规则化);语言:无限且开放集合(于是必然产生语病)\n\
好像谁说:语言的边界就是思想的边界。\n\
'
print(cont) # when I packing
print('There is a bug when I am packing')
player = [0.5, 0.5, 0.1, 0.1, 0.1, 0.1, 0.8]
# with open('../media/language', encoding='UTF-8') as file:
# cont = file.read()
idx = 0
for c in cont:
print(c, end='')
# winsound.Beep(5000, 100)
sleep(player[idx % len(player)])
idx += 1
# video('hello')
# text()
# winsound.Beep(5000, 100)
# winsound.MessageBeep(500)
def judge_time(name):
tim = strftime('%H:%M:%S', localtime())
hour = int(tim[:2])
if hour >= 21 or hour <= 3:
print('hello, 晚睡的' + name)
elif hour <= 8:
print('hello, 早起的' + name)
else:
print('hello, ' + name)
def log():
with open('log', 'a'):
pass
with open('log', 'r+', encoding='UTF-8') as file:
logs = file.read()
if len(logs) == 0:
name = input("Input your name(Then press Enter): ")
file.write(name + ' ' + strftime('%H:%M:%S', localtime()) + '\n')
return name
lines = logs.split('\n')
last_log = lines[-2].split(' ')
name, tim = last_log[0], last_log[1]
inp = input('Are you ' + name + '? (input yes/no, then press Enter):')
if inp == 'yes':
file.write(name + ' ' + strftime('%H:%M:%S', localtime()) + '\n')
return name
elif inp == 'no':
name = input("Input your name(Then press Enter): ")
file.write(name + ' ' + strftime('%H:%M:%S', localtime()) + '\n')
return name
else:
return -1
def roi():
pass
def camera():
cap = cv.VideoCapture(0, cv.CAP_DSHOW)
if not cap.isOpened():
cap.open()
frame_cnt = 0
start_t = int(time())
cr, cg, cb = 100, 100, 100
t_to2 = True
lower_face = array([0, 30, 60])
upper_face = array([20, 150, 255])
while True:
ret, frame = cap.read()
if not ret:
break
frame_cnt += 1
end_t = int(time())
if (end_t - start_t) == 2 and t_to2:
t_to2 = False
cv.imwrite('instant.png', frame)
logo = cv.imread('instant.png')
logo = cv.resize(logo, None, fx=0.2, fy=0.2)
logh, logw = logo.shape[0], logo.shape[1]
if (end_t - start_t) % 3 == 0:
cr, cg, cb = randint(0, 255), randint(0, 255), randint(0, 255)
h, w, c = frame.shape
left = w//3
right = w-w//3
cv.circle(frame, (40, 40), 20, (cb, cg, cr), -1)
frame[:h, :left, 0] = 0
# gray = cv.cvtColor(frame[:h, left: right], cv.COLOR_BGR2GRAY)
# gray[:, :] = power(2, log2((gray[:, :]+1)))
hsv = cv.cvtColor(frame[:h, left: right], cv.COLOR_BGR2HSV)
mask = cv.inRange(hsv, lower_face , upper_face)
face = cv.bitwise_and(frame[:h, left: right], frame[:h, left: right], mask=mask)
# cv.cvtColor(frame, cv.COLOR_BGR2GRAY, gray)
# cv.GaussianBlur(frame[:h, left: right], (5, 5), 0)
frame[:h, right:, 2] = 0
if not t_to2:
frame[:logh, w-logw:] = logo
cv.imshow('Tips: press q to quit', frame)
cv.imshow('face?', face)
x = cv.waitKey(1) # -1
if x == ord('q'):
break
# print('frame count: ', frame_cnt)
cap.release()
cv.destroyAllWindows()
user = log()
if user == -1:
exit(-1)
judge_time(user)
video = Thread(target=video, args=(user, 30))
video.setDaemon(True)
video.start()
# print("bg start")
text()
# print("text over")
camera()
input('Press any key to leave *o*')