Saving images in different folders using opencv - image

I've been trying to save images from my webcam in different folders but it doesnt work. what im trying to achieve is im trying to save 5 images per folder from my webcam but it doesnot work. Can somebody please help? Thanks
P.S here is my code
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
import os
import cv2
import time
video_capture = cv2.VideoCapture(0)
counter = 0
while(video_capture.isOpened()):
location = f'D:/DATA_SCIENCE/anand_fabrics/{counter}'
ret, frame = video_capture.read()
cv2.imwrite(os.path.join(location , f"frame{counter}.jpg"), frame)
if counter % 5 == 0:
createFolder(f'D:/DATA_SCIENCE/anand_fabrics/{counter}')
counter = counter + 1

You are saying you switch folder every 5 images, yet you change the path every single image:
while(video_capture.isOpened()):
location = f'D:/DATA_SCIENCE/anand_fabrics/{counter}'
...
counter = counter + 1
So basically you are trying to write to a non-existing path.
Try this:
video_capture = cv2.VideoCapture(0)
counter = 0
while(video_capture.isOpened()):
if counter % 5 == 0:
location = f'D:/DATA_SCIENCE/anand_fabrics/{counter}'
createFolder(loaction)
ret, frame = video_capture.read()
cv2.imwrite(os.path.join(location , f"frame{counter}.jpg"), frame)
counter = counter + 1

Related

What is a good way to draw a waveform with pyqt6?

Currently making an application which allows me to make a lightshow with some custom build LED-Controllers and for that i need to draw the waveform of the song on a widget.
Although I managed to do this it is still VERY slow (especially with .wav files longer than a few seconds). The thing is I don't know how to optimise this or if my approach is correct since i cant find anything on the web.
So my question is: what is the right way to go about this? How do audio editors display the waveform and are able to zoom in and out without lag?
So my current attempt at this is by using QGraphicsView and a QGraphicsScene, the latter one supposedly being made to represent a lot of custom graphics items.
The main function to look at here is drawWav() in class WavDisplay
Showcreator.py:
from PyQt6 import uic
from PyQt6.QtCore import (
QSize,
Qt
)
from PyQt6.QtGui import (
QAction,
QPen,
QPixmap,
QPainter,
QColor,
QImage
)
from PyQt6.QtWidgets import (
QMainWindow,
QWidget,
QStatusBar,
QFileDialog,
QGraphicsScene,
QGraphicsView,
QGridLayout
)
import sys
import wave
import pyaudio
import numpy as np
import threading
import soundfile as sf
import threading
class MainWindow(QMainWindow):
# audio chunk rate
CHUNK = 1024
def __init__(self):
super().__init__()
# set window title
self.setWindowTitle("LED Music Show")
# create file button
button_action = QAction("Open .wav file", self)
button_action.setStatusTip("Open a Wave file to the Editor.")
button_action.triggered.connect(self.openWav)
# set status bar
self.setStatusBar(QStatusBar(self))
# create menubar
menu = self.menuBar()
# add file button to status bar
file_menu = menu.addMenu("&File")
file_menu.addAction(button_action)
# create layout
layout = QGridLayout()
layout.setContentsMargins(0,0,0,0)
# create Wave display object
self.waveformspace = WavDisplay()
# add widget to layout
layout.addWidget(self.waveformspace, 0, 1)
self.centralWidget = QWidget()
self.centralWidget.setLayout(layout)
self.setCentralWidget(self.centralWidget)
def openWav(self):
# file selection window
self.filename, check = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","Wave files (*.wav)")
self.file = None
# try to open .wav with two methods
try:
try:
self.file = wave.open(self.filename, "rb")
except:
print("Failed to open with wave")
try:
self.file, samplerate = sf.read(self.filename, dtype='float32')
except:
print("Failed to open with soundfile")
# read file and convert it to array
self.signal = self.file.readframes(-1)
self.signal = np.fromstring(self.signal, dtype = np.int16)
# set file for drawing
self.waveformspace.setWavefile(self.signal)
self.waveformspace.drawWav()
# return file cursor to start
self.file.rewind()
# start thread for the player
# self.player = threading.Thread(target = self.playWav)
# try:
# self.player.daemon = True
# except:
# print("Failed to set player to Daemon")
# self.player.start()
except:
print("Err opening File")
def playWav(self):
lastFile = None
lastpos = None
p = pyaudio.PyAudio()
data = None
sampwidth = None
fps = None
chn = None
farmes = None
currentpos = 0
framespersec = None
while True:
if self.file != lastFile:
# get file info
sampwidth = self.file.getsampwidth()
fps = self.file.getframerate()
chn = self.file.getnchannels()
frames = self.file.getnframes()
lastFile = self.file
# open audio stream
stream = p.open(format = p.get_format_from_width(sampwidth), channels = chn, rate = fps, output = True)
# read first frame
data = self.file.readframes(self.CHUNK)
framespersec = sampwidth * chn * fps
print("file changed")
if self.pos != lastpos:
# read file for offset
self.file.readframes(int(self.pos * framespersec))
lastpos = self.pos
frames = self.file.getnframes()
print("pos changed")
while data and self.running:
# writing to the stream
stream.write(data)
data = self.file.readframes(self.CHUNK)
currentpos = currentpos + self.CHUNK
# cleanup stuff.
self.file.close()
stream.close()
p.terminate()
return
class WavDisplay(QGraphicsView):
file = None
maxAmplitude = 0
fileset = False
def __init__(self):
super().__init__()
def setWavefile(self, externFile):
self.file = externFile
self.fileset = True
# find the max deviation from 0 db to set draw borders
if max(self.file) > abs(min(self.file)):
self.maxAmplitude = max(self.file) * 2
else:
self.maxAmplitude = abs(min(self.file)) * 2
def drawWav(self):
# only draw when there is a set file
if self.fileset:
width = self.frameGeometry().width()
height = self.frameGeometry().height()
vStep = height / self.maxAmplitude
scene = QGraphicsScene(self)
# to draw on the middle of the widget
h = height / 2
# method 1 of drawing: looks at sections of the file and determines the max and min amplitude that would be visible on a single "column" of pixels and draws a vertical line between them
if width < len(self.file):
hStep = len(self.file) / width
drawArray = np.empty((width, 3))
for i in range(width - 1):
buffer = self.file[int(np.ceil(i * hStep)) : int(np.ceil((i + 1) * hStep))]
drawArray[i][0] = (min(buffer) * vStep) + h
drawArray[i][1] = (max(buffer) * vStep) + h
for i in range(width - 1):
self.line = scene.addLine(i, drawArray[i][0], i, drawArray[i][1])
# method 2 of drawing: this only happens when the amount of samples to draw is less than the windows width (e.g. when zoomed in and you can see the individual samples)
else:
hStep = width / len(self.file)
for i in range(len(self.file) - 1):
self.line = scene.addLine(i * hStep, int(self.file[i] * vStep + h), (i + 1) * hStep, int(self.file[i + 1] * vStep + h))
self.setScene(scene)
self.setContentsMargins(0,0,0,0)
self.show()
def resizeEvent(self, event) -> None:
# has to redraw the wave file if the window gets resized
self.drawWav()
# class not used yet
class effectList(QGraphicsView):
bpm = 130
trackBeats = 0
def __init__(self):
super().__init__()
def setBeatsAndBpm(self, trackLenght, Bpm):
self.bpm = Bpm
self.trackBeats = (trackLenght / 60) * self.bpm
main.py:
from PyQt6 import QtCore, QtGui, QtWidgets
from Showcreator import MainWindow
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec()
In essence: Where do i need to start to make this wave file view like one in for example Audacity? (Aka a fast rendering view which doesnt take ages)
Btw i have looked at seemingly duplicates of this question and as you can see in the code i have an algorythem that is only drawing as many lines as the window is wide and not all the 100000+ lines for each sample so the main problem i have should be the rendering method i guess.
Edit: I have all the data preloaded as im loading a wave file and convert it into a numpy array. And i need to display the file as a whole but be able to zoom in dynamically-

Read many videos and convert them into images

I have been able to get a program that can read a single video and convert it into images. However, actually, I am having many videos and I would like to write a function that can read the videos one by one and convert them into images. I aim to convert the videos into images so as to facilitate the processing. The code for converting a single video is as follows.
import sys
import argparse
import cv2
vidcap = cv2.VideoCapture('E:/DATA/1/12.mp4')
path_out = "E:/DATA_Synth/12"
success,image = vidcap.read()
#image=cv2.resize(image, (640, 480))
count = 0
while success:
cv2.imwrite(path_out + "/frame%03d.jpg" % count, image)
#cv2.imwrite(path_out + "/frame%d.jpg" % count, image)
success,image = vidcap.read()
#image = cv2.resize(image, (640, 480))
print ('Read a new frame: ', success)
count += 1
Any suggestions and comments would be highly appreciated
You should create a loop to iterate through your folder which contains all your videos and pick each video and run your above code.
import os
import sys
import argparse
import cv2
directory="enter your folder directory for video"
for vidfile in os.listdir(directory):
if vidfile.endswith('.mp4'):
vidcap=cv2.VideoCapture(os.path.join(directory, vidfile))
## write your code here for converting the video to individual frames
else:
continue
I have updated the codes as follows.
import os
import sys
import argparse
import cv2
directory="E:/Training/video"
path_out = "E:/DATA_Synth/12"
for vidfile in os.listdir(directory):
if vidfile.endswith('.avi'):
vidcap=cv2.VideoCapture(os.path.join(directory, vidfile))
## write your code here for converting the video to individual frames
success,image = vidcap.read()
#image=cv2.resize(image, (640, 480))
count = 0
while success:
#vidcap.set(cv2.CAP_PROP_POS_MSEC,(count*1000))
cv2.imwrite(path_out + "/frame%03d.jpg" % count, image) # save frame as JPEG file
#cv2.imwrite(path_out + "/frame%d.jpg" % count, image)
success,image = vidcap.read()
#image = cv2.resize(image, (640, 480))
#print ('Read a new frame: ', success)
count += 1
else:
continue
However, only the frames from the last video in my directory are saved. Please, how can I modify the codes in such a way that the name of frame as written here
cv2.imwrite(path_out + "/frame%d.jpg" % count, image)
may also contain the name of the corresponding video.

VideoCapture.read() returns past image

I'm running python3.6 with openCV on the Raspberry pi(OS is Raspbian)
The approximate structure of the code is as follows.
The image is captured at time interval(3~5 min).
Captured image is processed in functions and returns measure(kind of accuracy)
Iterate 1.~2. until end_check() returns True
Problem is that the most recent taken image is out of date. It looks it was taken almost 10 minutes ago. All images recently taken are late. But the images taken at the beginning seem to be timed. And the time recorded in all .jpg files entered correctly
+It looks this problem is occured after more than a hour. (20~22 iterates)
Images are captured with cam0.read() in the cv2 package. Below is main part of the code. It is quite long to upload full code. It someone request, I will update.
def run(interval,model_list):
cam0 = cv2.VideoCapture(0) #Only cam0 is used. cam2 is just to record.
camdir = "/home/pi/capstone/cam0/"
cam2 = cv2.VideoCapture(1)
cam2dir = "/home/pi/capstone/cam2/"
runNo = 0
acc_list = list()
error_list = list()
end = False
while(end == False):
print(runNo,"th run")
img_name = "%s.jpg" %runNo
frame, res = cam0.read() #`res` is the image which will be processed
cv2.imwrite(os.path.join(camdir,img_name),res)
_ , cam2pic = cam2.read()
cv2.imwrite(os.path.join(cam2dir,img_name),cam2pic)
try:
temp = Real(res)
mat = temp.match(model_list)
acc_list.append([mat,runNo])
print("Accuracy=", mat)
except ValueError:
acc_list.append(["ValueError",runNo])
error_list.append(["ValueError",runNo])
except AttributeError:
acc_list.append(["AttributeError", runNo])
error_list.append(["AttributeError",runNo])
except SmallObjectError:
acc_list.append(["SmallObjectError", runNo])
error_list.append(["SmallObjectError",runNo])
runNo = runNo+1
endNo = 40
if(runNo/2 > endNo):
end_check(res, end)
elif(runNo > endNo):
end = True
sleep(interval*60)
with open("acc_list.txt", "w") as output: #records for tracking errors
output.write(str(acc_list))
with open("err_list.txt", "w") as output:
output.write(str(error_list))
cam0.release()
cam2.release()
run(3.5,model_list)
(+) Some newly found things and guess
The images time gap is getting bigger with code running
Code finally showed OpenCV Error
It looks kind of Video signal is stored in RAM on R-pi and .read() returning out-dated image in RAM
Stored Video signal in RAM araise resource problem
Below is the OpenCV Error
OpenCV Error: Assertion failed (dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0)) in resize, file /home/pi/opencv/opencv-3.4.0/modules/imgproc/src/resize.cpp, line 4045
Traceback (most recent call last):
File "runpi.py", line 264, in <module>
run(3.5,model_list)
File "runpi.py", line 234, in run
mat = temp.match(model_list)
File "runpi.py", line 184, in match
self.__resize(model.get_m_inform())
File "runpi.py", line 147, in __resize
self.mask = cv2.resize(self.mask, None, fx=reratio, fy=reratio, interpolation = inter_method)
cv2.error: /home/pi/opencv/opencv-3.4.0/modules/imgproc/src/resize.cpp:4045: error: (-215) dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0) in function resize
(+) Some part of code araising Error
This is __.resize() method. When I process the image that occured OpenCV Error manually, it works well even if OpenCV Error pointing out kind of image size matter. So I thought it is not matter of image itself or size got from md_inf(). Anyway Here is code.
def __resize(self, md_inf):
#md_inf = [219, 122, 132, 171, 262] <-sample
reratio = md_inf[0]/self.y
if(reratio>1):
inter_method = cv2.INTER_LINEAR
else:
inter_method = cv2.INTER_AREA
###below is line 147###
self.mask = cv2.resize(self.mask, None, fx=reratio, fy=reratio, interpolation = inter_method)
temp = np.zeros((md_inf[3], md_inf[4]), np.uint8)
m_cx, m_cy = md_inf[1:3]
_, contour, _ = cv2.findContours(self.mask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
total_contour = contour[0]
for ctr in contour[1:]:
total_contour = np.concatenate((total_contour, ctr), axis =0)
mmt = cv2.moments(total_contour)
if(mmt['m00'] < 7500):
raise SmallObjectError
self.cy = int(mmt['m10']/mmt['m00']) #y is horrizon axis
self.cx = int(mmt['m01']/mmt['m00']) #x is vertical axis
x, y = self.mask.shape
adjust = m_cx - self.cx + x - temp.shape[0]
if(adjust > 0):
m_cx = m_cx - adjust
temp[(m_cx-self.cx):(m_cx-self.cx) +x, (m_cy-self.cy):(m_cy-self.cy) +y] = self.mask
self.mask = temp
I agree with Mark Serchell's comment. The way I am using is to set variable to time + x seconds and check. OpenCV have useful feature for skiping frames like cam.grab(). It will just read that frame from buffer but will not do anything with it. In that way you can avoid "suffering from buffering". Simple code would be:
import cv2
import time
cam = cv2.VideoCapture(url)
ret,frame = cam.read()
timeCheck = time.time()
future = 10*60 # delay
while ret:
if time.time() >= timeCheck:
ret,frame = cam.read()
# Do your staff here
timeCheck = time.time()+future
else:
# Read from buffer, but skip it
ret = cam.grab() # note that grab() function returnt only status code,not the frame

How do I convert a video or a sequence of images to a bag file?

I am new to ROS. I need to convert a preexisting video file, or a large amount of images that can be concatenated into a video stream, into a .bag file in ROS. I found this code online: http://answers.ros.org/question/11537/creating-a-bag-file-out-of-a-image-sequence/, but it says it is for camera calibration, so not sure if it fits my purpose.
Could someone with a good knowledge of ROS confirm that I can use the code in the link provided for my purposes, or if anyone actually has the code I'm looking for, could you please post it here?
The following code converts a video file to a bag file, inspired from the code in the link provided.
Little reminder:
this code depends on cv2 (opencv python)
time stamp of ROS message is calculated by frame index and fps. fps will be set to 24 if opencv unable to read it from the video.
import time, sys, os
from ros import rosbag
import roslib, rospy
roslib.load_manifest('sensor_msgs')
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
TOPIC = 'camera/image_raw/compressed'
def CreateVideoBag(videopath, bagname):
'''Creates a bag file with a video file'''
bag = rosbag.Bag(bagname, 'w')
cap = cv2.VideoCapture(videopath)
cb = CvBridge()
prop_fps = cap.get(cv2.CAP_PROP_FPS)
if prop_fps != prop_fps or prop_fps <= 1e-2:
print "Warning: can't get FPS. Assuming 24."
prop_fps = 24
ret = True
frame_id = 0
while(ret):
ret, frame = cap.read()
if not ret:
break
stamp = rospy.rostime.Time.from_sec(float(frame_id) / prop_fps)
frame_id += 1
image = cb.cv2_to_compressed_imgmsg(frame)
image.header.stamp = stamp
image.header.frame_id = "camera"
bag.write(TOPIC, image, stamp)
cap.release()
bag.close()
if __name__ == "__main__":
if len( sys.argv ) == 3:
CreateVideoBag(*sys.argv[1:])
else:
print( "Usage: video2bag videofilename bagfilename")

MatLab (Image Processing, Image Acquisition) How to save captured images by webcam without overwriting the original file name?

I want to save images without overwriting them whenever I hit the pushbutton. Can you please help me how save images without overwriting the original? What I want to do is whenever I'll hit the pushbutton, It will generated 1 image at a time without deleting the original.
Just like in digital cameras, whenever I will hit the trigger button, it will save 1 image and the file name will be image1.jpg. So basically, if I will push trigger again, it will capture 1 image again and the file name will be image2.jpg and so on.
here is my code:
counter = 1; %initialize filename increment
vid = videoinput('winvideo',2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
savename = strcat('C:\Users\Sony Vaio\Documents\Task\images\image_' ,num2str(counter), '.jpg'); %this is where and what your image will be saved
imwrite(img, savename);
counter = counter +1; %counter should increment each time you push the button
My code saves and keeps on overwriting the filename image1.jpg.
To make things clear
1 push to the pushbutton, 1 image saves.
it's like it will call the whole block code every hit at pushbutton.
I hope you guys can help me. I really troubled right now :(
Thank you :)
If this is the code that makes up the callback function for that pushbutton, then yes indeed, it will execute the entire block every time you push it.
If that is the case, you'll need to change it to this:
%// initialize filename increment
persistent counter;
if isempty(counter)
counter = 1; end
vid = videoinput('winvideo', 2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
%// this is where and what your image will be saved
savename = [...
'C:\Users\Sony Vaio\Documents\Task\images\image_', ...
num2str(counter), '.jpg'];
imwrite(img, savename);
%// counter should increment each time you push the button
counter = counter + 1;
or, you could check what files are actually present, and use the next logical filename in the sequence:
vid = videoinput('winvideo', 2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
%// this is where and what your image will be saved
counter = 1;
baseDir = 'C:\Users\Sony Vaio\Documents\Task\images\';
baseName = 'image_';
newName = [baseDir baseName num2str(counter) '.jpg'];
while exist(newName,'file')
counter = counter + 1;
newName = [baseDir baseName num2str(counter) '.jpg'];
end
imwrite(img, newName);
Every time you push that button the counter value resets to 1 because of the very first statement:
counter = 1
and hence the error.
counter = length(dir('*.jpg')) + 1; %Counts the number of .jpg files in the directory
That should do the job.
Zaher:
I'm online program about image processing and image acquisition from the camera in writing MATLAB.
When receiving the image every few seconds I get a picture of the camera.
Photos must be stored and processed in statistical process control charts.
When the first image after image acquisition program hangs and stops.
Please code to get images every 10 seconds online from cameras send images that can be used in statistical process control.
Thank

Resources