你能幫我解決我的代碼問題嗎,我想將我使用 opencv 創建的資料附加到 csv 檔案中。我在網上看了幾個教程,但結果不是我想要的,這里是完整的代碼
import cv2
import os
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from csv import writer
faceCascade = cv2.CascadeClassifier(
cv2.data.haarcascades 'haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier(
cv2.data.haarcascades 'haarcascade_eye.xml')
font = cv2.FONT_HERSHEY_SIMPLEX
sampel_foto = 5
counter = 1
jarak = 50
dir_name = 'C:/MyDrive'
video_capture = cv2.VideoCapture(1)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
frame_copy = frame.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray, scaleFactor=1.2, flags=cv2.CASCADE_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x w, y h), (0, 0, 255), 3)
roi_gray = gray[y:y h, x:x w]
roi_color = frame[y:y h, x:x w]
cv2.putText(frame, 'face: ' str(w) ',' str(h),
(10, 30), font, 0.6, (255, 255, 255), 2)
eyes = eyeCascade.detectMultiScale(roi_gray)
# Draw a rectangle around the eyes
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex ew, ey eh), (0, 255, 0), 2)
cv2.putText(frame, 'eyes: ' str(ew) ',' str(eh),
(10, 50), font, 0.6, (255, 255, 255), 2)
if cv2.waitKey(1) & 0xFF == ord('c'):
cv2.imwrite(f'{dir_name}{jarak}.{counter}.jpg', frame_copy)
print(f'simpan foto ke-{counter} dengan jarak: {jarak} cm!')
counter = 1
if counter > sampel_foto:
break
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
# Get list of all files only in the given directory
imfilelist = [os.path.join(dir_name, f)
for f in os.listdir(dir_name) if f.endswith(".jpg")]
#list_of_files = filter( lambda x: os.path.isfile(os.path.join(dir_name, x)), os.listdir(dir_name) )
# Sort list of files based on last modification time in ascending order
list_of_files = sorted(
imfilelist, key=lambda x: os.path.getmtime(os.path.join(dir_name, x)))
for el in list_of_files:
imagen = cv2.imread(el)
gray = cv2.cvtColor(imagen, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = faceCascade.detectMultiScale(
gray, scaleFactor=1.2, flags=cv2.CASCADE_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(imagen, (x, y), (x w, y h), (0, 255, 0), 2)
roi_gray = gray[y:y h, x:x w]
roi_color = imagen[y:y h, x:x w]
roi_color = imagen[y:y h, x:x w]
# Detect eyes
eyes = eyeCascade.detectMultiScale(roi_gray)
# Draw a rectangle around the eyes
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex ew, ey eh), (0, 255, 0), 2)
cv2.putText(imagen, '', (x ex, y ey), 1, 1, (0, 255, 0), 1)
data = str(w) ',' str(h) ',' str(ew) ',' str(eh) ',' str(jarak)
print(data)
# Create a file object for this file
with open('test.csv', 'a') as f_object:
# Pass this file object to csv.writer()
writer_object = writer(f_object)
writer_object.writerow(data_pixel)
#Close the file object
f_object.close()
問題是當我運行腳本將資料添加到 csv 時,結果不是我想要的,這是示例:
2,5,3,",",2,5,3,",",5,0,",",5,0,",",5,0
據說我想要的結果如下:
268,268,96,96,50
258,258,60,60,50
260,260,102,102,50
我該怎么做才能解決我的問題?非常感謝您的幫助。
uj5u.com熱心網友回復:
我假設您正在嘗試寫入data您的 CSV 檔案?如果是這樣,將檔案創建移動到for回圈上方,如下所示:
with open('test.csv', 'a', newline='') as f_output:
csv_output = csv.writer(f_output)
for el in list_of_files:
.
.
.
csv_output.writerow([w, h, ew, eh, jarak])
您不需要顯式關閉檔案,因為它with()會自動處理。因此,對于您提供的代碼,它將是:
import cv2
import os
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from csv import writer
faceCascade = cv2.CascadeClassifier(
cv2.data.haarcascades 'haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier(
cv2.data.haarcascades 'haarcascade_eye.xml')
font = cv2.FONT_HERSHEY_SIMPLEX
sampel_foto = 5
counter = 1
jarak = 50
dir_name = 'E:/OneDrive - Institut Teknologi Sepuluh Nopember/Kuliah Teknik Elektro/Semester 3/SEC/Tugas_SEC_Devis/dataset_foto/50/'
video_capture = cv2.VideoCapture(1)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
frame_copy = frame.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray, scaleFactor=1.2, flags=cv2.CASCADE_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x w, y h), (0, 0, 255), 3)
roi_gray = gray[y:y h, x:x w]
roi_color = frame[y:y h, x:x w]
cv2.putText(frame, 'face: ' str(w) ',' str(h),
(10, 30), font, 0.6, (255, 255, 255), 2)
eyes = eyeCascade.detectMultiScale(roi_gray)
# Draw a rectangle around the eyes
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex ew, ey eh), (0, 255, 0), 2)
cv2.putText(frame, 'eyes: ' str(ew) ',' str(eh),
(10, 50), font, 0.6, (255, 255, 255), 2)
if cv2.waitKey(1) & 0xFF == ord('c'):
cv2.imwrite(f'{dir_name}{jarak}.{counter}.jpg', frame_copy)
print(f'simpan foto ke-{counter} dengan jarak: {jarak} cm!')
counter = 1
if counter > sampel_foto:
break
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
# Get list of all files only in the given directory
imfilelist = [os.path.join(dir_name, f)
for f in os.listdir(dir_name) if f.endswith(".jpg")]
#list_of_files = filter( lambda x: os.path.isfile(os.path.join(dir_name, x)), os.listdir(dir_name) )
# Sort list of files based on last modification time in ascending order
list_of_files = sorted(
imfilelist, key=lambda x: os.path.getmtime(os.path.join(dir_name, x)))
# Create a file object for this file
with open('test.csv', 'a') as f_object:
# Pass this file object to csv.writer()
writer_object = writer(f_object)
for el in list_of_files:
imagen = cv2.imread(el)
gray = cv2.cvtColor(imagen, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = faceCascade.detectMultiScale(
gray, scaleFactor=1.2, flags=cv2.CASCADE_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(imagen, (x, y), (x w, y h), (0, 255, 0), 2)
roi_gray = gray[y:y h, x:x w]
roi_color = imagen[y:y h, x:x w]
roi_color = imagen[y:y h, x:x w]
# Detect eyes
eyes = eyeCascade.detectMultiScale(roi_gray)
# Draw a rectangle around the eyes
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex ew, ey eh), (0, 255, 0), 2)
cv2.putText(imagen, '', (x ex, y ey), 1, 1, (0, 255, 0), 1)
writer_object.writerow([w, h, ew, eh, jarak])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/365301.html
標籤:Python 文件 附加 opencv-python
上一篇:將多個JSON檔案轉換為CSV檔案,每個檔案在一列中
下一篇:java中決議布林值的奇怪行為
