I'm currently trying to improve python/openCV performances using tmpfs, as i'm stuck around 5-10 FPS while trying to record a USB camera on a raspberry Pi 3, at 640x480.
On my system, i get these raw write transfer rates, using dd command: SD Card : 2.5Mo/s tmpfs : 380 Mo/s
Sadly, when writing my opencv video file on a tmpfs folder, i didn't get any improvement. Here is my benchmark code :
import cv2
import numpy as np
import time
x = np.random.randint(255, size=(480, 640,3)).astype('uint8')
writer1 = cv2.VideoWriter('/home/pi/testDX1.avi',cv2.cv.CV_FOURCC('D','X','5','0'), 10, (640, 480), True)
ctime=time.time()
for i in range(250):
writer1.write(x)
writer1.release()
print("SDCard took : "+`time.time()-ctime`)
writer2 = cv2.VideoWriter('/var/tmp/testDX2.avi',cv2.cv.CV_FOURCC('D','X','5','0'), 10, (640, 480), True)
ctime=time.time()
for i in range(250):
writer2.write(x)
writer2.release()
print("tmpfs took : "+`time.time()-ctime`)
It gives :
SDCard took : 8.289990901947021 tmpfs took : 8.240657806396484
The tmpfs is well enabled, as stated by mount command :
mount | grep "/var/tmp"
gives tmpfs on /var/tmp type tmpfs (rw,nosuid,nodev,relatime)
mount | grep "/ "
gives /dev/mmcblk0p2 on / type ext4 (rw,noatime,data=ordered)
Did someone know why tmfs isn't improving write speed ?
If writing to memory directly does not improve speed, then it is likely not the bottleneck. Your CPU usage is probably at 100% while encoding.
As the ultimate test, you can try saving to /dev/null. According to dd that has a throughput of about 5GB/s on my laptop. Though to do that you will need to make a symlink to /home/pi/null.avi as openCV wants a file that ends with .avi.
I tested your script on my laptop on SSD, HDD, tmpfs, and /dev/null and they all gave roughly the same result (using 2500 instead of 250 iterations).
/dev/null took : 6.05289101600647
tmpfs took : 6.018815994262695
HDD took : 5.695630073547363
SSD took : 5.711660861968994