Question: Im currently using python3 for my code. I cant figure out how I can make my terminal output create a file(example: csv file or txt
Im currently using python3 for my code. I cant figure out how I can make my terminal output create a file(example: csv file or txt file), then put/write the output of my data there, then close the file after all data is collected. It prints out these lines: print("BRISQUE:",brisqueval) and print("NIQUE:", skvideo.measure.niqe(vid)[0]). So my question is how do I create a file where it can write in the data (seperated by a comma then starts new line for next frame) and then after all data is written close the file?? Below is what I have so far... What would I have to change or add to do this?
import cv2 import numpy import os import subprocess #from brisque import BRISQUE import skvideo.io import skvideo.measure import numpy as np from os import path import sys import csv
# Inits #brisq = BRISQUE()
# Playing video from file: cap = cv2.VideoCapture('demo_yt_2d.mp4')
try: if not os.path.exists('data'): os.makedirs('data') except OSError: print ('Error: Creating directory of data')
currentFrame = 0 while(currentFrame < 3): # Capture frame-by-frame ret, frame = cap.read()
# Saves image of the current frame in jpg file name = './data/frame' + str(currentFrame) + '.bmp' print ('Creating...' + name) cv2.imwrite(name, frame)
# Insert metrics here # In this case, use the 'regular' BRISQUE_revised binary for comparison purposes completedProcess = subprocess.run(['./brisquequality', "-im", name], stdout=subprocess.PIPE, universal_newlines=True) stdoutReturn = completedProcess.stdout.splitlines() brisqueval = stdoutReturn[0].split(":")[1] print("BRISQUE:",brisqueval)
vid = skvideo.io.vread(name, outputdict={"-pix_fmt": "gray"})[:, :, :, 0] vid = skvideo.utils.vshape(vid) print("NIQUE:", skvideo.measure.niqe(vid)[0])
# Future use built-in BRISQUE metric #print('Reference image: %s' % brisq.get_score(name)) # delete file if os.path.exists(name): os.remove(name) print("Deleting", name) else: break
# To stop duplicate images currentFrame += 1
# When everything done, release the capture cap.release() cv2.destroyAllWindows()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
