Pythony应用(01)-学习监控(05)

UC03–翻看电脑桌面历史记录

US10-显示历史图片文件列表:

  1. Web服务对象MonitorPageWebService:
  • save_capture_pic_path 图片保存位置
  • get_capture_his_pic() 获取历史图片文件名列表
  • get_html() 中div(displayFileName)显示文件名,使用一个单元格显示文件列表,点击链接显示图片

class MonitorPageWebService:
    ......
    save_capture_pic_path = os.path.join(
        os.path.dirname(__file__), "static", "capture_pic")
    ......
    @classmethod
    def get_capture_his_pic(cls):
        his_filename_list = os.listdir(cls.save_capture_pic_path)
        his_filename_list.sort(reverse=True)
        return his_filename_list

    @classmethod
    def get_html(cls, form_field):
        html = """
    ......

                <tr>
                    <td>
                    <div style="height:600px; width:100%; overflow:auto">
                    <div>
                    {% for filename in his_filename_list %}
                        <a href="javascript:void(0);" οnclick="fa_click(this)"> 
                        {{ filename }} </a> <BR>
                    {% endfor %}
                    </div>
                    </div>
                    </td>
                    
        """

        handlers = {
    ......
            "hisPic": {"value": "hisPic", "text": "历史屏幕图片",
                       "checked": "", "imgSrc": url_for('new_capture_pic')},
    ......
        }
    ......

US11-显示图片:

  1. Web服务对象MonitorPageWebService:
  • save_capture_pic_path 图片保存位置
  • get_html() 中div(displayFileName)显示文件名,使用一个单元格显示文件列表,点击链接显示图片
  • his_capture_pic()获取指定文件的图片
  • main_run.his_capture_pic()url路由

class MonitorPageWebService:
    ......
    save_capture_pic_path = os.path.join(
        os.path.dirname(__file__), "static", "capture_pic")
    ......
    @classmethod
    def get_html(cls, form_field):
        html = """
    ......
                <table border = 0>
                <tr>
                <td colspan="2">
                <div id="displayFileName" style="text-align:center">
                
                </div>
                </td>
                </tr>
                <tr>
                    <td>
                    <div style="height:600px; width:100%; overflow:auto">
                    <div>
                    {% for filename in his_filename_list %}
                        <a href="javascript:void(0);" οnclick="fa_click(this)"> 
                        {{ filename }} </a> <BR>
                    {% endfor %}
                    </div>
                    </div>
                    </td>
                    
                    <td>
                        <img id="displayPic" src="{{ cur_handler.imgSrc }}" 
                        width="100%" height="100%" alt="monitor capture" 
                        οnclick="img_display_pic_onclick(this)">
                    </td>
                    
                </tr>
                
                </table>
            </form>
                <script type="text/javascript">
                    function fa_click(elem){ //file_a_link_on_click
                        var fileName = elem.innerText;
                        var img = document.getElementById('displayPic');
                        img.src = './his_capture_pic/' + fileName + '/';
                        var div = document.getElementById('displayFileName');
                        div.innerHTML = '<H2>' + fileName + '</H2>';
                    }
                </script>
            </body>
            </html>
        """

        handlers = {
    ......
            "hisPic": {"value": "hisPic", "text": "历史屏幕图片",
                       "checked": "", "imgSrc": url_for('new_capture_pic')},
    ......
        }
    ......

    @classmethod
    def his_capture_pic(cls, filename):
        if not filename:
            filename_list = os.listdir(cls.save_capture_pic_path)
            if filename_list:
                filename = filename_list[0]
            else:
                filename = os.path.join(cls.save_capture_pic_path,
                                        "..", "favicon.ico")
        new_filename = os.path.join(cls.save_capture_pic_path, filename)
        return send_file(new_filename, mimetype='image/png')

    @classmethod
    def main_run(cls):
    ......

        @app.route('/his_capture_pic/<filename>/')
        def his_capture_pic(filename=""):
            return cls.his_capture_pic(filename)
    ......

UC04–远程查看孩子电脑的摄像头

US12-摄像头图片获取:

  1. 摄像头对象:VideoCamera
  • get_frame() 获取一帧

class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0)

    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, image = self.video.read()
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()
    ......

US13-显示摄像头视频流:

  1. Web服务对象:MonitorPageWebService
  • get_html() 使用img显示视频流
  • camera_gen():产生一帧图片
  • camera_video_feed(): 返回前台一帧图片
  • main_run.camera_video_feed():URL路由


class MonitorPageWebService:
    ......

    @classmethod
    def get_html(cls, form_field):
        html = """
    ......
                    <td>
                        <img id="displayPic" src="{{ cur_handler.imgSrc }}" 
                        width="100%" height="100%" alt="monitor capture" 
                        οnclick="img_display_pic_onclick(this)">
                    </td>
    ......
            </body>
            </html>
        """

        handlers = {
    ......
            "realTimeCamera": {
                "value": "realTimeCamera", "text": "实时摄像头画面",
                "checked": "", "imgSrc": url_for('camera_video_feed')},
        }
    ......

    @staticmethod
    def camera_gen(camera):
        while True:
            frame = camera.get_frame()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

    @classmethod
    def camera_video_feed(cls):
        """视频流的路线。将其放在img标记的src属性中。"""
        return Response(cls.camera_gen(VideoCamera()),
                        mimetype='multipart/x-mixed-replace; boundary=frame')

    ......
    @classmethod
    def main_run(cls):
    ......
        @app.route('/camera_video_feed')
        def camera_video_feed():
            return cls.camera_video_feed()

    ......

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mengyoufengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值