Python-openCV

提供:sufeeWiki
ナビゲーションに移動 検索に移動

概要

画像編集・変換や、カメラを制御する優れもの

インストール

  1. openCVのインストール
    1. debian等
apt install openCV
  1. Python
pip install opencv-python

使用例

  • データ格納庫
class data:
    def __init__(self):
        self.buffer = [b"", b""]
        self.e = [False, False]
        self.f = False

    def set(self, d, val=0):
        self.buffer[val] = (b"--frame\r\n"+
                b"Content-Type: image/jpeg\r\n\r\n" + d + b"\r\n")

    def get(self, val=0):
        return self.buffer[val]

    def start(self, val=0):
        self.e[val] = False

    def finish(self, val=0):
        self.e[val] = True

    def end(self, val=0):
        return self.e[val]

    def setFinal(self):
        self.f = True

    def final(self):
        return self.f

datas = data()

ビデオ録画

jpgでフレーム保存

def video0(threadedname, d):
    print("video0起動")
    mnt = 0
    vid0 = cv2.VideoCapture(-1)
    while not(d.final() or d.end()):
        while(vid0.isOpened()):
            try:
                dt_now = datetime.datetime.now()
                img, frame = vid0.read()
                cv2.putText(frame, f"{dt_now.hour}:{dt_now.minute}:{dt_now.second}", (
                    0, 50), cv2.FONT_HERSHEY_PLAIN, 1.5, (220, 20, 60), 1, cv2.LINE_AA)
                if dt_now.hour >= 18:
                    frame = cv2.LUT(frame, lookUpTable)
                ret, frame = cv2.imencode('.jpg', frame)
                d.set(d=frame.tobytes(), val=mnt)
            except RuntimeError:
                d.setFinal(mnt)
                break
            except KeyboardInterrupt:
                d.finish(0)
                break
            except Exception as e:
                print(e)
            if(d.final() or d.end()):
                break
            time.sleep(1/30)
    print("終了")
    try:
        vid0.release()
    except:
        print("異常終了")

変換

準備中

http送信

def gen(vid=0,sleep=1/30):
    while not(datas.final() or datas.end()):
        try:
            vm = datas.get(vid)
            yield vm
            time.sleep(sleep)
        except:
            pass

#フレーム取得
@app.route('/live')
def video_feed():
    return Response(gen(),
                    mimetype="multipart/x-mixed-replace; boundary=frame")