「Python-openCV」の版間の差分
ナビゲーションに移動
検索に移動
Sufee Admin (トーク | 投稿記録) タグ: 手動差し戻し |
Sufee Admin (トーク | 投稿記録) |
||
(同じ利用者による、間の4版が非表示) | |||
7行目: | 7行目: | ||
# Python | # Python | ||
pip install | pip install opencv-python | ||
== 使用例 == | |||
* データ格納庫 | |||
<syntaxhighlight lang="python3"> | |||
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() | |||
</syntaxhighlight> | |||
=== ビデオ録画 === | |||
jpgでフレーム保存 | |||
<syntaxhighlight lang="python3"> | |||
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("異常終了") | |||
</syntaxhighlight> | |||
=== 変換 === | |||
準備中 | |||
=== http送信 === | |||
<syntaxhighlight lang="python3"> | |||
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") | |||
</syntaxhighlight> |
2022年2月22日 (火) 16:24時点における最新版
概要
画像編集・変換や、カメラを制御する優れもの
インストール
- openCVのインストール
- debian等
apt install openCV
- 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")