python - OpenCV,cherrypy,使用按键在浏览器中显示图像

标签 python python-2.7 opencv cherrypy

我知道这会有点奇怪,请容忍我问这个愚蠢的问题,而且是一种糟糕的编码风格,因为我知道不能在构造函数中声明所有内容并同时使用它们。

我的目标是上传静态图像内容。我正在使用库 opencv'cherrypy'

虽然opencv部分会被用来显示图片,使用cherrypy的原因是在浏览器上传图片,使用waitKey(0),我想刷新这些图片.

问题是这不起作用,而且我知道设计很差。

我发布这个是因为,我现在没有解决方案,并且想在这方面得到一些帮助,即使是一个小提示也可以,我会自己更正代码。

代码如下:

import sys
import cherrypy
import cv2
import base64


class Img(object):

    def __init__(self):


        self.plat = sys.platform

        if self.plat == 'win32':

            self.file_dir = "C:\\Users\\user\\Desktop\\fruit\\"

        elif self.plat == 'linux2':

            self.file_dir = "/host/Users/user/Desktop/fruit/"

        print self.plat
        print self.file_dir

        self.img   = cv2.imread(self.file_dir + "orange.jpg")



    @cherrypy.expose

    def index(self):


        self.k = cv2.waitKey(0)

        if self.k == 'w':

            # self.img   = cv2.imread(self.file_dir + "orange.jpg")

            _, data     =  cv2.imencode('.jpeg', self.img)

            jpeg_base64 = base64.b64encode(data.tostring())

            return """
            <html>
            <head>
            <title>Fruit Nutritional Information</title>
            </head>
            <html>
            <body>
            <img src='data:image/jpeg;base64,%s' height = 640 width = 1200 />
            </body>
            </html
            """ % (jpeg_base64)

        elif self.k == ord('q'):
            sys.exit()



if __name__ == '__main__':
    cherrypy.config.update({'server.socket_host': '127.0.0.1', 'server.socket_port':8080 })
    cherrypy.quickstart(Img())

最佳答案

您可以提供一个接受键盘输入的 JavaScript 文件: serving static content with CherryPy 或者只是将 JavaScript 代码放在 HTML 代码的脚本标签中。

关于python - OpenCV,cherrypy,使用按键在浏览器中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22913261/

相关文章:

c++ - 唯一标识 OpenCV 相机

python - 在python中重置打印功能

python - 为什么在 Windows 上启动新进程时 Python 的多处理模块会导入 __main__ ?

python - 如何找到图像中数字的中心坐标

python - 获取文件复制到文件夹的时间(Python)

python - smtplib.SMTP 无法登录

c++ - 将 cv::Points2f 的 vector 与 cv::Matx33f 相乘

python - matplotlib 导入时需要时间

python - numpy bincount - 选择最大权重而不是对所有权重求和

python - rospy 中的实时 2D 激光扫描仪数据