Python:通过摄像头抓取图像并自动上传至新浪微博

最后更新于:2022-04-01 19:39:37

通过上一篇文章[《Python:通过摄像头实现的监控功能》](http://blog.csdn.net/dyx1024/article/details/7243956),突然想到将每次采集到的图片可以直接上传至微博,然后可以通过手机准实时查看要监控的地方,实现思路如下: a.程序A 每30s通过摄像头采集一次图像,并保存; b.程序B也是每30s将采集到的图片上传至新浪微博; 实现如下:  1. 采集图像程序A: ~~~ #!/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: ~~~ #!/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() ~~~ 3. 测试 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-08_5757935565c64.gif) [](http://blog.csdn.net/dyx1024/article/details/7243956)
';