简体   繁体   English

OpenCv 修改后不保存图片

[英]OpenCv wont't save the image after modification

Simply I am feeding the model the image and then putting a text on the image of the class the image belongs to, but the images are not showing up in my directory.简单地说,我正在为 model 提供图像,然后在图像所属的 class 的图像上放置一个文本,但图像没有显示在我的目录中。

My code:我的代码:

import cv2
import os
from tensorflow.keras.models import load_model
import numpy as np


roi_path = 'other/DetectedFaces/'
output_path = 'other/output/'
model = load_model('weights/my_model')
text = ''
font = cv2.FONT_HERSHEY_SIMPLEX


for index, image in enumerate(os.listdir(roi_path)):
    img = cv2.imread(os.path.join(roi_path, image))
    img = np.expand_dims(img, 0)
    

    try:
        prediction = np.argmax(model.predict(img))
        
        if prediction == 0:
            text = 'confused'
        elif prediction == 1:
            text = 'happy'
        elif prediction == 2:
            text = 'neutral'
        elif prediction == 3:
            text = 'none'
        elif prediction == 4:
            text = 'sad'
        elif prediction == 5:
            text = 'yawn'
            
    except: 
        print('Model prediction failed!')
        continue
            
    cv2.putText(img, text, (24, 85), font, 0.3, (0,255,0))
    cv2.imwrite(os.path.join(output_path,str(index)+'.jpg'), img)
    
    print('Successfully Predicted!'

The output: output:

Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!
Successfully Predicted!

And this is what I get in my output directory这就是我在 output 目录中得到的

在此处输入图像描述

After processing thousands of images all I get is one '.jpg' file that I can't even open.在处理了数千张图像后,我得到的只是一个我什至无法打开的“.jpg”文件。

I am running on Ubuntu 20.04.我在 Ubuntu 20.04 上运行。

import cv2
import os
from tensorflow.keras.models import load_model
import numpy as np


roi_path = 'other/DetectedFaces/'
output_path = 'other/output/'
model = load_model('weights/my_model')
text = ''
font = cv2.FONT_HERSHEY_SIMPLEX


for index, image in enumerate(os.listdir(roi_path)):
    img = cv2.imread(os.path.join(roi_path, image))
    imgM = np.expand_dims(img, 0)


    try:
        prediction = np.argmax(model.predict(imgM))
    
        if prediction == 0:
            text = 'confused'
        elif prediction == 1:
            text = 'happy'
        elif prediction == 2:
            text = 'neutral'
        elif prediction == 3:
            text = 'none'
        elif prediction == 4:
            text = 'sad'
        elif prediction == 5:
            text = 'yawn'
        
    except: 
        print('Model prediction failed!')
        continue
        
    cv2.putText(img, text, (24, 85), font, 0.3, (0,255,0))
    cv2.imwrite(os.path.join(output_path,str(index)+'.jpg'), img)

    print('Successfully Predicted!'

Your image has been changed here img = np.expand_dims(img, 0)您的图像已在此处更改img = np.expand_dims(img, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM