save_path=r"E:\myTool\appium_xiangmu\test_video" ts=calendar.timegm(time.gmtime()) videoname = str(ts)+ ".mp4" save_file_path = '{}\{}'.format(save_path, videoname) #保存视频 fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 不同视频编码对应不同视频格式(例:'I','4','2','0' 对应avi格式) video = cv2.VideoWriter(save_file_path, fourcc, 20, (int(1080/3),int(2340/3))) while True: image=get_imgdecdoe() video.write(image) video.release() #释不完全如何修改代码
时间: 2024-03-13 14:42:43 浏览: 22
为了确保视频释放完全,可以将 `video.release()` 放在一个 `try...finally` 语句块中,即使发生异常也能够释放视频。修改后的代码如下所示:
```
import cv2
import calendar
import time
save_path = r"E:\myTool\appium_xiangmu\test_video"
ts = calendar.timegm(time.gmtime())
videoname = str(ts) + ".mp4"
save_file_path = '{}\{}'.format(save_path, videoname)
# 保存视频
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(save_file_path, fourcc, 20, (int(1080/3),int(2340/3)))
try:
while True:
image = get_imgdecdoe()
video.write(image)
finally:
video.release()
```
这样即使程序发生异常退出,视频也能够完全释放。
相关问题
import subprocess import cv2 import numpy as np import time import calendar #从手机获取二进制图片 def get_app_img(): # 从ADB获取屏幕图像 try: output = subprocess.check_output('adb exec-out screencap -p', shell=True) # 处理 output 中的数据 except subprocess.CalledProcessError as e: print('Error:', e) except Exception as e: print('Unexpected error:', e) return output #获取每一张图片的三维数据 def get_imgdecdoe(): output = get_app_img() # print("------output-----{}".format(output)) # 将输出转换为图像 image1 = cv2.imdecode(np.fromstring(output, dtype='uint8'), cv2.IMREAD_COLOR) # print(image) # #缩小图片的大小 image = cv2.resize(image1, (int(1080 / 3), int(2340 / 3))) return image def app_video(): save_path=r"E:\myTool\appium_xiangmu\test_video" ts=calendar.timegm(time.gmtime()) videoname = str(ts)+ ".mp4" save_file_path = '{}\\{}'.format(save_path, videoname) #保存视频 fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 不同视频编码对应不同视频格式(例:'I','4','2','0' 对应avi格式) video = cv2.VideoWriter(save_file_path, fourcc, 5, (int(1080/3),int(2340/3))) try: while True: image = get_imgdecdoe() # 显示图像 cv2.imshow('Screen', image) # 按下ESC键退出循环 if cv2.waitKey(1) == 27: break image=get_imgdecdoe() video.write(image) finally: video.release() #释放 print("________视频处理完毕_______:视频号显示:{}".format(videoname)) cv2.destroyAllWindows() if __name__ == '__main__': app_video(),缺少录制时用户的点击的提示怎么处理,有实例代码吗
如果想要在录制的视频中添加用户的点击提示,可以使用cv2.circle()函数在视频中绘制一个圆圈来表示用户的点击。具体实现可以在获取到每一张图片的时候,判断手机屏幕是否有点击事件发生,如果有,则在该图片上绘制一个圆圈。以下是一个示例代码:
```
import subprocess
import cv2
import numpy as np
import time
import calendar
# 从手机获取二进制图片
def get_app_img():
# 从ADB获取屏幕图像
try:
output = subprocess.check_output('adb exec-out screencap -p', shell=True)
# 处理 output 中的数据
except subprocess.CalledProcessError as e:
print('Error:', e)
except Exception as e:
print('Unexpected error:', e)
return output
# 获取每一张图片的三维数据
def get_imgdecdoe():
output = get_app_img()
# 将输出转换为图像
image1 = cv2.imdecode(np.fromstring(output, dtype='uint8'), cv2.IMREAD_COLOR)
# 缩小图片的大小
image = cv2.resize(image1, (int(1080 / 3), int(2340 / 3)))
return image
# 在图片上绘制圆圈
def draw_circle(image, x, y):
# 设置圆圈的颜色和半径
color = (0, 0, 255)
radius = 10
# 在图片上绘制圆圈
cv2.circle(image, (x, y), radius, color, -1)
def app_video():
save_path=r"E:\myTool\appium_xiangmu\test_video"
ts=calendar.timegm(time.gmtime())
videoname = str(ts)+ ".mp4"
save_file_path = '{}\\{}'.format(save_path, videoname)
#保存视频
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 不同视频编码对应不同视频格式(例:'I','4','2','0' 对应avi格式)
video = cv2.VideoWriter(save_file_path, fourcc, 5, (int(1080/3),int(2340/3)))
try:
while True:
# 获取当前的屏幕图像
image = get_imgdecdoe()
# 判断是否有点击事件发生
# 这里假设点击事件的坐标为 (x, y)
if 点击事件发生:
# 在图片上绘制圆圈
draw_circle(image, x, y)
# 显示图像
cv2.imshow('Screen', image)
# 按下ESC键退出循环
if cv2.waitKey(1) == 27:
break
# 将图片写入视频文件
video.write(image)
finally:
# 释放视频文件
video.release()
print("________视频处理完毕_______:视频号显示:{}".format(videoname))
cv2.destroyAllWindows()
if __name__ == '__main__':
app_video()
```
在上面的代码中,我们定义了一个 `draw_circle()` 函数,用于在图片上绘制圆圈。然后在每次获取到屏幕图像时,判断是否有点击事件发生,如果有,则在该图片上绘制一个圆圈。最后将图片写入视频文件即可。
帮我检查一下makefile文件mytool:test.o gcc test.o -o mytool test.o:test.c gcc test.c -c -Wall -g -o test.o有问题吗
根据你提供的Makefile文件,存在一些问题:
1. `test.o`规则中的命令行存在重复,应该只保留`gcc test.c -c -Wall -g -o test.o`,而不是将`test.o`作为目标文件并再次编译。
2. `mytool`规则中的命令行中应该使用`$^`代替目标文件列表,这样可以自动引用依赖项列表,而不必手动列出每个源文件。
因此,可以将Makefile文件修改为以下内容:
```
mytool: test.o
gcc $^ -o $@
test.o: test.c
gcc -c -Wall -g $< -o $@
```
在修改后的文件中,`mytool`规则使用了`$^`代替目标文件列表,`test.o`规则只编译`test.c`文件并生成`test.o`目标文件。这样可以正确构建目标文件和可执行文件,并且在需要更新源文件时可以自动重新编译。