flask에 opencv tensorflow 카메라 실행 코드 배포 2

reenact_l·2023년 6월 11일
0

server

목록 보기
8/11

이전 이야기

추가 참고자료

해당 코드로 진행하였을때 주소 실행하면 무한 로딩
종료하면 여러가지 문제가 발생함

이를 토대로 코드를 수정함

기존 app.py

cv2.imshow('Korean Sign-language', image)

        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
    """def video_feed():
		return Response(get_frames())"""
   	if __name__ == '__main__':
		app.run(debug=True)

수정 app.py

 cv2.imshow('Korean Sign-language', image)

            if cv2.waitKey(10) & 0xFF == ord('q'):
                break

            res, buffer = cv2.imencode('.jpg', image)
            frame = buffer.tobytes()
            yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


@app.route('/video_feed')
def video_feed():
    return Response(get_frames(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
	app.run(host='0.0.0.0')

수정 html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>sign-language</title>
</head>
<body>
    <div class="container">
        <div class ="row">
            <div class = "col-lg-8 offst-lg-2">
                <img src ="{{url_for('video_feed')}}" width="100%">
            </div>
        </div>
    </div>
</body>
</html>

실행 결과

수정한 내용
1. get_frame()의 값을 받아올 함수가 없다는 것을 확인 ->video_feed()함수 재작
1.1 Response는 flask import 시 같이 진행
1.2 get_frame()를 mimetype으로 respones

  1. get_frame에서 카메라가 실행되어야 할거라고 판단
    2.2 opencv 카메라의 경우 jpg로 받아들여진다 확인
    2.3 res, buffer = cv2.imencode('.jpg', image)로 인코드 값을 줌
    2.4 버퍼변수의 tobytes를 frame으로 주고 해당 값을 yield 진행
  • tobytes == numpy에서 특정 숫자를 byte단위로 변환해주는 메소드
    yield == 결과값을 반환하는 함수 little bit different form return

https://github.com/jhlee343/2wol3il

profile
icantdoanything

0개의 댓글