通過上一篇文章
《Python:通過攝像頭實現的監控功能
》見 http://www.linuxidc.com/Linux/2012-02/54206.htm,突然想到將每次采集到的圖片可以直接上傳至微博,然後可以通過手機准實時查看要監控的地方,實現思路如下:
a.程序A 每30s通過攝像頭采集一次圖像,並保存;
b.程序B也是每30s將采集到的圖片上傳至新浪微博;
實現如下:
1. 采集圖像程序A:
[python]
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
-
- from VideoCapture import Device
- import time
-
- #最多保存5張抓取到的圖片,超過5張,覆蓋最早的那一張,依次循環
- MAX_PIC_NUM = 5
-
- #抓取頻率,30秒抓取一次
- SLEEP_TIME_LONG = 30
-
- #初始化攝像頭
- cam = Device(devnum=0, showVideoWindow=0)
-
- iNum = 0
- while True:
-
- #抓圖
- cam.saveSnapshot(str(iNum)+ '.jpg', timestamp=3, boldfont=1, quality=75)
-
- #休眠一下,等待一分鐘
- time.sleep(SLEEP_TIME_LONG)
-
- #超過5張,則覆蓋之前的,否則,硬盤很快就會寫滿
- if iNum == MAX_PIC_NUM:
- iNum = 0
- else:
- iNum += 1
-
2. 上傳圖片到新浪微博程序B:
[python]
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
-
- from weibopy.auth import OAuthHandler
- from weibopy.api import API
- import ConfigParser
- import time
-
- MAX_PIC_NUM = 5
- SLEEP_TIME_LONG = 30
-
- def press_sina_weibo():
- '''''
- 調用新浪微博Open Api實現通過命令行寫博文,功能有待完善
- author: socrates
- date:2012-02-06
- 新浪微博:@沒耳朵的羊
- '''
- sina_weibo_config = ConfigParser.ConfigParser()
- #讀取appkey相關配置文件
- try:
- sina_weibo_config.readfp(open('sina_weibo_config.ini'))
- except ConfigParser.Error:
- print 'read sina_weibo_config.ini failed.'
-
- #獲取需要的信息
- consumer_key = sina_weibo_config.get("userinfo","CONSUMER_KEY")
- consumer_secret =sina_weibo_config.get("userinfo","CONSUMER_SECRET")
- token = sina_weibo_config.get("userinfo","TOKEN")
- token_sercet = sina_weibo_config.get("userinfo","TOKEN_SECRET")
-
- #調用新浪微博OpenApi(python版)
- auth = OAuthHandler(consumer_key, consumer_secret)
- auth.setToken(token, token_sercet)
- api = API(auth)
-
- #通過命令行輸入要發布的內容
- # weibo_content = raw_input('Please input content:')
- # status = api.update_status(status=weibo_content)
- # print "Press sina weibo successful, content is: %s" % status.text
- iNum = 0
- while True:
- #上傳圖片,名稱和內容如果重復,open api會檢查,內容采用了取當前時間的機制
- #圖片名稱從0-5循環遍歷
- status = api.upload(str(iNum)+ '.jpg', time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))
- time.sleep(SLEEP_TIME_LONG)
-
- if iNum == MAX_PIC_NUM:
- iNum = 0
- else:
- iNum += 1
-
- if __name__ == '__main__':
- press_sina_weibo()