質問編集履歴

1

教えていただいた物をもとにコードを書き足しました

2022/07/01 14:05

投稿

ngh_orange
ngh_orange

スコア17

test CHANGED
File without changes
test CHANGED
@@ -103,3 +103,52 @@
103
103
 
104
104
  どなたか教えていただけると幸いです
105
105
 
106
+
107
+ ### 追記 07/01
108
+ 教えていただきありがとうございます
109
+ genという関数に引数を一つ渡し、その引数でcamera.pyのほうにストリーミングか撮影かを判断させるように改造してみました
110
+ しかし、そもそもカメラが見つからないという「can't open camera by index」のエラーがでてその先が止まってしまいます
111
+ camera.pyの中身です
112
+ ```Python
113
+ class Camera(object):
114
+ def __init__(self):
115
+ self.video = cv2.VideoCapture(0)
116
+
117
+ def __del__(self):
118
+ self.video.release()
119
+
120
+ def get_frame(self,v):
121
+ success, image = self.video.read()
122
+ if v == 0:
123
+ ret, frame = cv2.imencode('.jpg', image)
124
+ return frame
125
+ else:
126
+ cv2.imwrite('test.jpg', image)
127
+ ```
128
+ app.pyの、一部です。ほかは上記のapp.pyと変わりません
129
+ genに引数を渡す処理だけ加えています
130
+ ```python
131
+ @app.route("/ccaptureh")
132
+ def ccapture():
133
+ print("ccaptureh!!!!!!!!!!!")
134
+ Response(gen(Camera(),1))
135
+ return render_template("stream.html")
136
+ ```
137
+ app.pyのgenの改造後です
138
+ ```python
139
+ def gen(camera,v):
140
+ print(v)
141
+ while True:
142
+ frame = camera.get_frame(v)
143
+ if v == 1:break
144
+
145
+ if frame is not None:
146
+ yield (b"--frame\r\n"
147
+ b"Content-Type: image/jpeg\r\n\r\n" + frame.tobytes() + b"\r\n")
148
+ else:
149
+ print("frame is none")
150
+ ```
151
+
152
+ 教えていただいたとおりに、インスタンスを作成し、引数を渡して判断するようにしてみましたが
153
+ ストリーミングは今まで通り成功するのですが、撮影が成功しません。どのように直せばいいでしょうか?
154
+