OpenCV无法在Flask应用程序中读取文件

时间:2019-04-27 12:19:55

标签: python opencv flask

我有一个flask应用程序,该应用程序允许用户上传图像,然后执行一些多步骤处理。

我正在尝试使用cv2.imread在cv2中读取上传的图像,但是该函数从不返回它,只是挂在cv2.imread

调用方法

def remove_skin_color(filename):
    print('removing skin color for {}'.format(filename))
    colorDetection = ColorDetector()
    img_loc = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename)
    print('processing image {}'.format(img_loc))
    no_skin_img = colorDetection.get_removed_skin(img_loc=img_loc)
    cv2.imwrite(os.path.join(app.root_path, IMG_FOLDER, filename), no_skin_img)
    print('writing image done')
    return filename

日志

[Sat Apr 27 14:05:53.119646 2019] [wsgi:error] [pid 12364:tid 1328] [client 197.125.110.218:39284] request method is POST\r
[Sat Apr 27 14:05:53.122573 2019] [wsgi:error] [pid 12364:tid 1328] [client 197.125.110.218:39284] True\r
[Sat Apr 27 14:05:53.122573 2019] [wsgi:error] [pid 12364:tid 1328] [client 197.125.110.218:39284] receiving ...  file.JPEG\r
[Sat Apr 27 14:05:53.122573 2019] [wsgi:error] [pid 12364:tid 1328] [client 197.125.110.218:39284] 1556366753_file.JPEG\r
[Sat Apr 27 14:05:53.126479 2019] [wsgi:error] [pid 12364:tid 1328] [client 197.125.110.218:39284] removing skin color for 1556366753_file.JPEG\r
[Sat Apr 27 14:05:53.126479 2019] [wsgi:error] [pid 12364:tid 1328] [client 197.125.110.218:39284] processing image C:\\wamp64\\www\\app\\uploads\\1556366753_file.JPEG\r

被调用的方法

 def get_removed_skin(self, bn_img=None, img_loc=None):
        try:
            if img_loc:
                sourceImage = cv2.imread(img_loc) #code hangs here
            else:
                sourceImage = bn_img

        except Exception as e:

            print(e)

        # Constants for finding range of skin color in YCrCb
        min_YCrCb = np.array([0, 133, 77], np.uint8)
        max_YCrCb = np.array([255, 173, 127], np.uint8)

        # Convert image to YCrCb
        imageYCrCb = cv2.cvtColor(sourceImage, cv2.COLOR_BGR2YCR_CB)

        # Find region with skin tone in YCrCb image
        skinRegion = cv2.inRange(imageYCrCb, min_YCrCb, max_YCrCb)

        # Do contour detection on skin region
        _, contours, hierarchy = cv2.findContours(skinRegion, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        cv2.fillPoly(sourceImage, pts=contours, color=(255, 255, 255))

        return sourceImage

注释:正在Wamp(Apache)服务器+笔记本电脑上的WSGI上运行此应用程序

虚拟主机配置

# Virtual Hosts
#
<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  ErrorLog ${INSTALL_DIR}/www/app/app.log
   WSGIScriptAlias / "${INSTALL_DIR}/www/app/web.wsgi"
  DocumentRoot "${INSTALL_DIR}/www/app"
  <Directory "${INSTALL_DIR}/www/app/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews +ExecCGI
    AllowOverride All
    #Require local
    Require all granted
  </Directory>
</VirtualHost>

当我在本地运行该应用程序时,它可以按预期运行,没有任何问题

我已经简化了以下流程,并且仍然是同一问题

def upload_file():
    print('request method is {}'.format(request.method))
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            print('file not in request.files')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            print('filename is {}'.format(file.filename))
            return redirect(request.url)
        print(file and allowed_file(file.filename))
        if file and allowed_file(file.filename):
            print('receiving ... ', file.filename)
            filename = secure_filename(file.filename)
            ts = int(time.time())
            file_name = file_name_template.format(ts, filename)
            print(file_name)
            filePath = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], file_name)

            file.save(filePath)
            # file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name))

            img = cv2.imread(filePath) 
            print(' file has been read') #this line never gets printed
             out_image_name = remove_skin_color(file_name)

            json_data = color_palette(out_image_name)

            return json_data

1 个答案:

答案 0 :(得分:0)

解决方案是在虚拟主机文件中添加WSGIScriptAlias application-group=%{GLOBAL} 问题是

  

具有未设计用于子解释器的扩展模块。以上迫使它在主解释器中运行。

请参考thisthis