AHK调用opencv(五)保存视频

;调用opencv录制视频并且保存为文件
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
hOpencv := DllCall("LoadLibrary", "str", "opencv_world455.dll", "ptr")
hOpencvCom := DllCall("LoadLibrary", "str", "autoit_opencv_com455.dll", "ptr")
DllCall("autoit_opencv_com455.dll\DllInstall", "int", 1, "wstr", A_IsAdmin = 0 ? "user" : "", "cdecl")
cv  := ComObjCreate("OpenCV.cv")
cap := ComObjCreate("OpenCV.cv.VideoCapture")
;fourcc := ComObjCreate("OpenCV.cv.VideoWriter_fourcc")
out := ComObjCreate("OpenCV.cv.VideoWriter")
codec := out.fourcc(asc("M"), asc("J"), asc("P"), asc("G")) ;这里函数接收char类型数据,所以用Tebayaki建议的asc()函数
NumPut(fps := 30, temp := 0, "double")
fps := NumGet(temp, "double")
; msgbox, % type(fps)
; type(v) {
;     if IsObject(v)
; 	{
; 		if ObjGetCapacity(v)>ObjCount(v)
; 			return "associative array"
; 		else
; 			return "array"
; 	}
;     return v="" || [v].GetCapacity(1) ? "String" : InStr(v,".") ? "Float" : "Integer"
; }
out.open("output.avi", codec, fps, ComArrayMake([640,480]))
frame := ComObjCreate("OpenCV.cv.MAT")
cap.open(0)
ComArrayMake(inputArray)
{
	arr := ComObjArray(VT_VARIANT:=12, inputArray.Length())
	Loop,% inputArray.Length()
	{
		arr[A_Index-1] := inputArray[A_Index]
	}
	return arr
}
While cap.isOpened(){
    ret := cap.read(frame)
	if ret{
		frame := cv.flip(frame, 1) ;0为翻转180度,1不翻转
		out.write(frame)
		cv.imshow("frame", frame)
	}
}
cap.release()
out.release()
cv.destroyAllWindows()

上面用到的dll和图片蓝奏云链接

 

天黑版opencv_ahk.dll使用(改变了调用方式,优化速度...)

相关文件:https://wwz.lanzouw.com/iAkK803eaaud

播放视频依赖opencv_videoio_ffmpeg455_64.dll,链接https://wwz.lanzouw.com/iBP3A03ed1vi密码:fcd2

cv2.ahk和log.ahk来自社区群友zzZ...

可以用文件中的天黑版的v2h版ahk运行。

示例:保存视频

#DllLoad opencv_ahk.dll
#include <cv2>
#include <log>
SetWorkingDir A_ScriptDir
;初始化opencv模块
cv := ObjFromPtr(DllCall('opencv_ahk.dll\opencv_init', 'ptr', DllCall(A_AhkPath '\ahkGetApi', 'ptr'), 'cdecl ptr'))
cap := cv.VideoCapture()
frame := cv.Mat()
out := cv.VideoWriter()
codec := out.fourcc(Ord('M'), Ord('J'), Ord('P'), Ord('G'))
out.open("output.avi", codec, 30.0, [640, 480])
cap.open(0)
result := cap.isOpened()
while result{
    ret := cap.read(frame)
    if ret{
        out.write(frame)
        cv.imshow("image", frame)
    }
}
cap.release()
out.release()
cv.destroyAllWindows()

有错误请联系我改正!

本系列所有贡献者(AutoHotKey中文社区群友)不分先后:天黑请闭眼,zzZ...,演好自己,僵尸,城西,Tebayaki。

posted @ 2022-10-12 09:55  c/  阅读(138)  评论(0编辑  收藏  举报