Rather, why does it output an empty first column to csv when I call to_csv
. I'm not certain if the dataframe actually contains an extra column or merely outputs one to csv. I have the code below, and when I open its output, times.csv
, I get an "empty" first column:
,Start,End
0,2020-05-23 20:27:39.388228,2020-05-23 20:27:43.24543
import cv2, time, pandas
from datetime import datetime
first_frame=None
status_list=[]
times=[]
df=pandas.DataFrame(columns=["Start","End"])
video=cv2.VideoCapture(0)
while True:
check, frame = video.read()
status=0
grey=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
grey=cv2.GaussianBlur(grey,(21,21),0)
if first_frame is None:
first_frame=grey
continue
delta_frame=cv2.absdiff(first_frame, grey)
thresh_frame=cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_frame=cv2.dilate(thresh_frame, None, iterations=2)
(cnts,_)=cv2.findContours(thresh_frame.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour)<10000:
continue
status=1
(x,y,w,h)=cv2.boundingRect(contour)
cv2.rectangle(frame,(x,y), (x+w,y+h), (0,255,0), 3)
status_list.append(status)
if len(status_list)>=2:
if status_list[-1]==1 and status_list[-2]==0:
times.append(datetime.now())
if len(status_list)>=2 and status_list[-1]==0 and status_list[-2]==1:
times.append(datetime.now())
cv2.imshow("Capturing", grey)
cv2.imshow("Delta", delta_frame)
cv2.imshow("Threshold", thresh_frame)
cv2.imshow("Colour Frame", frame)
key=cv2.waitKey(1)
if key==ord('q'):
if status==1:
times.append(datetime.now())
break
print(status_list)
print(times)
for i in range(0,len(times),2):
df=df.append({"Start":times[i],"End":times[i+1]},ignore_index=True)
df.to_csv(r"MotionDetector\times.csv")
video.release()
cv2.destroyAllWindows()
我怀疑这与数据帧的索引有关,但是除了简单的两列网格之外,我没有指定任何其他需要的东西。
The additional column you see is the index of the dataframe. You can switch it off in the output by
index=False
: