강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của myc007786
myc007786

câu hỏi đã được viết

Chương trình Raspberry Pi IoT và khóa học giới thiệu về đám mây

카메라 연동관련 질문..

Viết

·

942

0

안녕하세요,,

라즈베리파이에 usb카메라를 2대 이상 연결해서 확인하는 방법은 없을까요?

from flask import Flask, render_template, Response

import cv2

app = Flask(__name__)

camera_indexes = [0, 2, 6] # 카메라 인덱스 리스트

def generate_frames(camera_index):

capture = cv2.VideoCapture(camera_index)

if not capture.isOpened():

raise IOError(f"Cannot open camera {camera_index}")

while True:

ret, frame = capture.read()

if not ret:

break

ret, jpeg = cv2.imencode('.jpg', frame)

frame = jpeg.tobytes()

yield (b'--frame\r\n'

b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route("/")

def index():

return render_template('cameratest0621.html', camera_indexes=camera_indexes)

@app.route("/video_feed/<int:camera_index>")

def video_feed(camera_index):

return Response(generate_frames(camera_index),

mimetype='multipart/x-mixed-replace; boundary=frame')

if name == "__main__":

app.run(host="0.0.0.0", port=8080)

<!DOCTYPE html>

<html>

<head>

<title>Camera Streams</title>

</head>

<body>

{% for index in camera_indexes %}

<div>

<img src="{{ url_for('video_feed', camera_index=index) }}">

</div>

{% endfor %}

</body>

</html>

소스코드인데,, 카메라 1대씩은 가능한데, 2대 이상은 안나옵니다..ㅠㅠ

Raspberry-Piiot

Câu trả lời 1

0

안녕하세요, 인프런 AI 인턴이에요.

라즈베리파이에서 2대 이상의 usb카메라를 연결하여 사용하는 것은 가능해요. 하지만 각 카메라마다 다른 인덱스를 가지고 있으므로, 코드에서 인덱스 리스트를 수정해야 할 필요가 있어요.

예를 들어, 3개의 카메라를 연결했을 경우 각각의 인덱스가 0, 1, 2일 때, camera_indexes 리스트를 다음과 같이 수정하면 됩니다.

camera_indexes = [0, 1, 2]

그리고 generate_frames 함수에서 사용하는 capture 객체도 카메라 인덱스에 따라 다르게 설정해야 합니다.

capture = cv2.VideoCapture(camera_index)

이 부분을 다음과 같이 수정하면 됩니다.

capture = cv2.VideoCapture(camera_indexes[camera_index])

위의 수정 예시대로 수정하시면 2대 이상의 카메라를 연결해서 사용할 수 있습니다. 해당 코드에서는 flask를 통해 연결한 카메라들을 html 상에서 확인할 수 있게 되어있습니다.

참고하시면 도움이 되실 것 같아요. 감사합니다.

Hình ảnh hồ sơ của myc007786
myc007786

câu hỏi đã được viết

Đặt câu hỏi