0
如何正確設置全局變量?python和全局變量
# default objects is being set as None (null), after importing the piCamera library
camera = None
之後,我宣佈攝像頭類的GET
定義:
class Webcam:
def GET(self):
web.header('Content-type','multipart/x-mixed-replace; boundary=--jpgboundary')
stream=io.BytesIO()
start=time.time()
for foo in camera.capture_continuous(stream,'jpeg'):
web.header('Content-type','--jpgboundary')
web.header('Content-type','image/jpeg')
web.header('Content-length',len(stream.getvalue()))
yield (stream.getvalue())
stream.seek(0)
stream.truncate()
time.sleep(.5)
if __name__ == "__main__":
camera = picamera.PiCamera()
global camera
camera.resolution = (640, 480)
app = web.application(urls, globals())
app.internalerror = web.debugerror
app.run()
我應該把if __name__ == "__main__":
我定義了網絡攝像頭前級?
這裏是我的電話追蹤:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 1245, in communicate
req.respond()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 775, in respond
self.server.gateway(self).respond()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 2018, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/httpserver.py", line 306, in __call__
return self.app(environ, xstart_response)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/httpserver.py", line 274, in __call__
return self.app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 281, in wsgi
result = peep(result)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 261, in peep
firstchunk = iterator.next()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 605, in wrap
yield next()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 597, in next
return result.next()
File "/home/pi/piApp/index.py", line 77, in GET
for foo in camera.capture_continuous(stream,'jpeg'):
AttributeError: 'NoneType' object has no attribute 'capture_continuous'
全球關鍵字只讓你分配到模塊中的全局範圍較小。它在這裏什麼都不做,因爲如果語句不創建範圍。 – geoffspear
@Wooble:你能否建議我另一種選擇?問題是我想從不同的課程中訪問相機。 –