前提・実現したいこと
Pythonにおける出力結果(動画.avi)の保存方法について
出力される動画が保存されないコードになっていますが、これを修正していただきたいです。
何行に何を書く、というように具体的な回答が嬉しいです。
発生している問題・エラーメッセージ
初学者ゆえに、手がつけられません。
エラーメッセージ:なし
該当のソースコード
Python
ソースコード
import cv2
import numpy as np
import os
import csv
import matplotlib.pyplot as plt
def mouse_event(event, x, y, flags, params):
if event == cv2.EVENT_MOUSEMOVE:
cv2.line(frame, (x, 0), (x, h-1), (255, 255, 255), 1)
cv2.line(frame, (0, y), (w-1, y), (255, 255, 255), 1)
cv2.imshow(name, frame)
if event == cv2.EVENT_LBUTTONDOWN: position = (x, y) print(x, y) data.append([x, y]) return x, y
def edge_left(center_px, w, thr_L):
Edge_L = []
for i in range(w-1, 0, -1):
if center_px[i] > thr_L:
Edge_L.append(i)
return Edge_L
def edge_right(center_px, w, thr_R):
Edge_R = []
for i in range(w-1, 0, -1):
if center_px[i] > thr_R:
Edge_R.append(i)
return Edge_R
def frm_show(frame, Edge_L, Edge_R, name):
cv2.line(frame, (Edge_L[0], 0), (Edge_L[0], h-1), (255, 0, 0), 1)
cv2.line(frame, (Edge_R[0], 0), (Edge_R[0], h-1), (0, 0, 255), 1)
cv2.imshow(name, frame)
return
movie = input("DRUG&DROP YOUR FiLE(AVI):")
movie = movie.lstrip("'").rstrip("'")
name = os.path.basename(movie)
t_sta = input("TYPE START TIME (s): ")
t_sta = int(t_sta)
t_fin = input("TYPE FINISH TIME (s): ")
t_fin = int(t_fin)
thr_L = int(238)
thr_R = input("temperature(℃): ")
thr_R = float(thr_R)*0.6036
thr_R = int(thr_R)
cap = cv2.VideoCapture(movie)
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = round(cap.get(cv2.CAP_PROP_FPS))
cnt = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.release()
data = []
cap = cv2.VideoCapture(movie)
while (cap.isOpened()):
ret, frame = cap.read()
cv2.namedWindow(name, cv2.WINDOW_NORMAL)
cv2.imshow(name, frame)
cv2.setMouseCallback(name, mouse_event)
if cv2.waitKey(25)&0xFF == 13: break
cv2.destroyAllWindows()
y = data[1][1] - data[0][1]
dy = 20 / y#1ピクセルあたりの長さ
center_y = int(data[0][1] + y/2)#試料中心軸のy座標
frm_sta = t_sta * fps
frm_fin = t_fin * fps
frm = []
left = []
right = []
for n in range(frm_sta, frm_fin,1):
cap.set(cv2.CAP_PROP_POS_FRAMES, n)
ret, frame = cap.read()
frm_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)#BGR→RGB
frm_gray = cv2.cvtColor(frm_rgb, cv2.COLOR_RGB2GRAY)#グレースケール化
center_px = frm_gray[center_y, :] #試料中心の画素値の読取
Edge_L = edge_left(center_px, w, thr_L)
Edge_R = edge_right(center_px, w, thr_R)
frm_show(frame, Edge_L,Edge_R,name)
frm.append(n)
left.append(Edge_L[0])#左の端のx座標
right.append(Edge_R[0])#右の端のx座標
if cv2.waitKey(1) & 0xFF == 27: print("ESCAPED") break if n >= frm_fin: print("FINISHED") break
cap.release()
cv2.destroyAllWindows()
frm = np.array(frm)
left = np.array(left)
right = np.array(right)
time = []
Length = []
time = (frm[:]-frm_sta)/fps
Left = leftdy
Right = rightdy
Length = Right - Left
output = np.array([time, Left, Right, Length])
output = output.T
func_l = np.poly1d(np.polyfit(time, Left,1))
a_l, b_l = np.polyfit(time, Left, 1)
Left2 = []
Left2 = a_ltime[:]+b_l
func_r = np.poly1d(np.polyfit(time, Right,1))
a_r, b_r = np.polyfit(time, Right, 1)
Right2 = []
Right2 = a_rtime[:]+b_r
ave_L = np.mean(Length)
f = open(movie.rstrip(".avi") + "_FL.csv", "w")
f_writer = csv.writer(f, lineterminator = '\n')
f_writer.writerow(["Ave.PL (mm)", ave_L])
f_writer.writerow(["Time","Left","Right","Length"])
f_writer.writerow(["(sec)", "(mm)","(mm)","(mm)"])
f_writer.writerows(output)
f.close()
plt.rcParams["font.family"] = "Arial"
plt.rcParams["figure.figsize"] = [5,5]
plt.rcParams["xtick.direction"] = "in"
plt.rcParams["ytick.direction"] = "in"
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.text(0.33,0.93,"Ave.PL="+str(round(ave_L,2))+"mm",
fontsize=13,transform=ax1.transAxes)
plt.plot(time, Left, linestyle="None", marker="o", markeredgecolor="red",
markerfacecolor="None", label="Flame Leading Edge", zorder=1)
plt.plot(time, Right, linestyle="None", marker="o", markeredgecolor="blue",
markerfacecolor="None", label="Flame Leading Edge", zorder=1)
plt.plot(time, Left2, "black", linestyle="dashed", zorder = 1)
plt.plot(time, Right2, "black", linestyle="dashed", zorder = 1)
plt.xlabel("Time(s)",fontsize=15)
plt.ylabel("Position(mm)", fontsize=15)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
plt.minorticks_on()
plt.tight_layout()
plt.savefig(movie.rstrip(".AVI")+"_PL.svg")
plt.show()
試したこと
見様見真似で試しましたが、成功しませんでした。
補足情報(FW/ツールのバージョンなど)
MacBook Pro2018
あなたの回答
tips
プレビュー