|
搞软件的,硬件没做什么特殊的diy
硬件:树莓派一枚 + usb 摄像头 没配置无线网卡,用的有线直接连到路由器
软件:
python + opencv
cap.py:
import os
import time
from logger import logger
from WeiboInterface import WeiboInterface
import cv
cap = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT, 240)
cv.SetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH, 320)
def cap_img(file):
frame = cv.QueryFrame(cap)
cv.SaveImage(file, frame)
return file
def remove_img(file):
if os.path.exists(file):
os.remove(file)
return True
else:
return False
logger = logger("main.log")
def main():
img_file = 'tmp.jpg'
weibo = WeiboInterface()
path = cap_img(img_file)
weibo.uploade_img(path)
remove_img(path)
#可以在此处设置一个间隔时间,循环向认证的微薄上传图片
if __name__ == '__main__':
main()
WeiboInterface.py:
app_key = 'xxxxxxx'
app_secret = 'xxxxxxxxxxxxxxxxxx'
r_url = 'https://api.weibo.com/oauth2/default.html'
#app_key 和 app_secret 从weibo开放平台申请一个应用后获得
#申请网址 http://open.weibo.com/apps
import time,os,sys
from logger import logger
logger = logger("main.log")
from weibo import APIClient # 从weibo 开放平台下载的api
class WeiboInterface:
def __init__(self):
self._client = APIClient(app_key, app_secret, r_url)
url = self._client.get_authorize_url()
print url
#在浏览器输入打印的url,然后会跳出一个登录的界面
#登录成功后把url中的code 复制过来,完成认证
code = raw_input("Input code:")
r = self._client.request_access_token(code)
self._client.set_access_token(r.access_token, r.expires_in)
def uploade_img(self, file_path):
self._ret_msg = self._client.upload.statuses__upload( \
status='uploaded at ' + str(time.time()),\
pic=open(file_path, 'rb'))
return self.parse_msg()
def parse_msg(self):
'''
TODO
'''
print self._ret_msg
return True
有感兴趣的可以一起研究
ps: 摄像头只能拍出320*240 的图片,否则会出现select time out的错误,而且质量很差,根本看不清,不知道是啥原因,电源,驱动,有人知道吗?
google了一下 某老外也遇到过,但是他的图片很清晰
http://fanjita.org/serendipity/archives/57-Capturing-webcam-video-with-OpenCV-on-Raspberry-Pi-Arch-Linux.html
|
|