본문 바로가기
소프트웨어/파이썬

[파이썬] ESP32-CAM 스트리밍 영상 불러오기(Tkinter, OpenCV)

by 만들오 2021. 7. 2.
728x90

 

 

ESP32-CAM의 스트리밍 영상을 파이썬으로 불러오는 코드입니다.

 

url 부분의 주소를 변경적용해야 합니다.

import cv2
import PIL.Image, PIL.ImageTk
from tkinter import *
import numpy as np
from urllib.request import urlopen


class App:
    def __init__(self, window):
        self.width, self.height = 640,480
        self.window = window
        self.window.geometry("640x480")
        self.window.title("Read ESP32-CAM")
        self.buffer = b''
        url = "http://192.168.0.12:81/stream" #Your url
        self.stream = urlopen(url)

        self.canvas = Canvas(window, width = self.width, height = self.height)
        self.canvas.pack()
        self.delay = 1
        self.update()
        self.window.mainloop()

    def update(self):
        while True:            
            self.buffer += self.stream.read(2560)
            head = self.buffer.find(b'\xff\xd8')
            end = self.buffer.find(b'\xff\xd9')
            try:
                if head > -1 and end > -1:
                    jpg = self.buffer[head:end+2]
                    self.buffer = self.buffer[end+2:]
                    frame = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
                    self.canvas.create_image(0, 0, image = self.photo, anchor = NW)
                    break
            except:
                pass

        self.window.after(self.delay, self.update)

App(Tk())

 

댓글