corrected output from 900+ csv to 1 with all RGB traces averages // split code into 3 functions

This commit is contained in:
Loic Delattre 2023-02-06 10:46:40 +01:00
parent c797336a8d
commit ee8de001ba
1 changed files with 42 additions and 27 deletions

View File

@ -1,27 +1,24 @@
import cv2
from PIL import Image
import csv
import os
import os.path
#create a directory to store the RGB datas ouptut
path_to_script = os.path.dirname(os.path.abspath(__file__))
if os.path.exists(path_to_script + r"\RGB database") == False:
os.mkdir(path_to_script + r"\RGB database")
def rep_files_init():
#create a directory to store the RGB datas ouptut
path_to_script = os.path.dirname(os.path.abspath(__file__))
if os.path.exists(path_to_script + r"\RGB database") == False:
os.mkdir(path_to_script + r"\RGB database")
onlyfiles = next(os.walk(path_to_script + r"\frames"))[2] #get the amount of frames previously extracted
frames_num = len(onlyfiles)-1
onlyfiles = next(os.walk(path_to_script + r"\frames"))[2] #get the amount of frames previously extracted
frames_num = len(onlyfiles)-1
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
image = "frames/frame1.jpg"
read_img = cv2.imread(image)
#cv2.imshow('img',imRGB)
#cv2.waitKey(0)
return frames_num
def RGB_dataframe(img, itr):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
@ -64,24 +61,42 @@ def RGB_dataframe(img, itr):
#cv2.imshow('img',imRGB)
#cv2.waitKey(0)
R = []
G = []
B = []
size_list = list(imRGB.shape[:2])
pixel_values = []
for i in range(0, size_list[0]):
for j in range(0, size_list[1]):
k = imRGB[i,j]
pixel_values.append(k)
fields = ['R', 'G', 'B']
with open('RGB database/RGB' + str(itr) + '_data.csv', 'w', newline="") as f:
R.append(k[0])
G.append(k[1])
B.append(k[2])
return R, G, B
def average_and_export():
i = 0
count = 0.1
pixel_values = []
frames_num = rep_files_init()
while i < frames_num:
i += 1
image = "frames/frame1.jpg"
read_img = cv2.imread(image)
out_colors = RGB_dataframe(read_img, i)
Ravg = sum(out_colors[0])/len(out_colors[0])
Gavg = sum(out_colors[1])/len(out_colors[1])
Bavg = sum(out_colors[2])/len(out_colors[2])
pixel_values.append([Ravg, Gavg, Bavg])
if i/frames_num >= count:
print(str(round(count*100))+ "% of the database exported to csv")
count += 0.1
with open('RGB database/RGB_data.csv', 'w', newline="") as f:
fields = ['R', 'G', 'B']
write = csv.writer(f)
write.writerow(fields)
write.writerows(pixel_values)
return
return
i = 0
count = 0.1
while i < frames_num:
i += 1
RGB_dataframe(read_img, i)
if i/frames_num >= count:
print(str(round(count*100))+ "% of the database exported to csv")
count += 0.1
average_and_export()