by BenTheElder · 6y ago · 0 comments
The inference / ml is expensive (which I did profile initially...), and I suspect not really optimized on this backend. It appears to be faster with webGL in the browser.
I sorta stopped worrying about it once it was "good enough" to show up to a few meetings with, but with all the attention I'll probably take another look.
It does look like someone ported bodypix to python, I'll probably try that next.
from keras.models import load_model
model = load_model('models/transpose_seg/deconv_bnoptimized_munet.h5', compile=False)
def get_mask(frame):
# Preprocess
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
simg = cv2.resize(frame, (128, 128), interpolation=cv2.INTER_AREA)
simg = simg.reshape((1, 128, 128, 3)) / 255.0
# Predict
out = model.predict(simg)
# Postprocess
msk = out.reshape((128, 128, 1))
mask = cv2.resize(msk, (frame.shape[1], frame.shape[0]))
return mask
The model file I got from:
https://github.com/anilsathyan7/Portrait-SegmentationYou can usually alter performance (with Bodypix that's an accuracy/speed tradeoff) or do something silly like downscale, run, and upscale the mask. I'd like to try this.
Amusingly I did some hacking on this and the current bottleneck is actually reading from the webcam which is capped at <10fps without doing anything else. Switching the capture to MJPG helps.