首页 文章 精选 留言 我的

精选列表

搜索[Python版本],共10000篇文章
优秀的个人博客,低调大师

python-opencv学习笔记(三)

Opencv提供了VideoCapture类和VideoWriter类来支持各种格式的视频文件。在到达视频文件末尾之前VideoCapture类可以通过read()函数来获取新的帧,每一帧是一幅BGR格式的图像。可将一幅传递给VideoWriter类的write()函数,该函数会将这幅图像加到VideoWriter类所指向的文件中下面是一个例子,读取MP4文件,并采用YUV颜色编码将其写入另一帧中: import cv2 videoCapture=cv2.VideoCapture('myvid.mp4') fps=videoCapture.get(cv2.CAP_PROP_FPS) size=(int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))) videoWriter=cv2.VideoWriter( 'MyOutPut.avi',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size ) success,frame=videoCapture.read() while success: videoWriter.write(frame) success,frame=videoCapture.read() 要注意:必须要为VideoWriter类的构造函数指定视频文件名,这个文件名对应的文件若存在,会被覆盖。也必须指定视频编解码器。编解码器的可能性根据系统不同而不同: cv2.VideoWriter_fourcc(‘I’,‘4’,‘2’,‘0’):该选项是一个未压缩的YUV颜色编码,是4:2:0色度子采集。这种编码兼容性较好,但文件较大,扩展名为.avi cv2.VideoWriter_foucc('P','I','M','I'):该选项是MPEG-1编码类型,扩展名为.avi cv2.VideoWriter_foucc('X','V','I','D'):该选项是MPEG-4编码类型,如果希望得到的视频大小为平均值,推荐使用此选项,文件扩展名.avi cv2.VideoWriter_foucc('T','H','E','O'):该选项Ogg Vorbis,文件扩展名应为.ogv。 cv2.VideoWriter_foucc('F','L','V','I'):该选项是一个Flash视频,文件扩展名应为.flv帧速率和帧大小也应该指定,因为需要从另一个视频复制视频帧,这些属性可以通过VideoCapture类的get()函数得到。 捕获摄像头帧:VideoCapture类可以获得摄像头的帧流。但对摄像头而言,通常不是用视频的文件名来构造VideoCapture类,而是需要传递摄像头的设备索引(device index)。 import cv2 cameraCapture=cv2.VideoCapture(0) fps=30 size=(int (cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int (cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))) videoWriter=cv2.VideoWriter( 'MyOutPutVid.avi',cv2.VideoWriter_fourcc('I','4','2','0'), fps,size ) success,frame=cameraCapture.read() numFramesRemaining=10*fps-1 while success and numFramesRemaining>0: videoWriter.write(frame) success,frame=cameraCapture.read() numFramesRemaining -=1 cameraCapture.release() 但VideoCapture类的get()方法不能返回摄像头速率的准确值,总是返回0。为了针对摄像头创建合适的VideoWriter类,要么对帧数率做出假设,要么使用计时器来测量,后一种更加好一些。摄像头的数目和顺序系统决定。但OpenCV没有提供任何查询摄像头数目和属性的方法。如果使用无效索引构造了VideoCapture类,就不会得到帧,VideoCapture的read()函数会返回(false,None)。为了不让read()函数从没有正确打开的VideoCapture类中获取数据,可在执行该函数之后使用VideoCa.isOpened方法一个判断,该方法返回一个Boolean值。当需要同步一组摄像头或多头(multihead)摄像头(例如立体摄像头或kinect)时,read()不合适,可用grab()和retrive()代替,可使用以下代码: success0=cameraCaputer0.grad() success1=cameraCapture1.grad() if success0 and success1: frame0=cameraCapture0.retrive() frame1=cameraCapture1.retrive()

优秀的个人博客,低调大师

python dataframe astype 字段类型转换

使用astype实现dataframe字段类型转换 # -*- coding: UTF-8 -*- import pandas as pd df = pd.DataFrame([{'col1':'a', 'col2':'1'}, {'col1':'b', 'col2':'2'}]) print df.dtypes df['col2'] = df['col2'].astype('int') print '-----------' print df.dtypes df['col2'] = df['col2'].astype('float64') print '-----------' print df.dtypes 1 2 3 4 5 6 7 8 9 10 11 12 13 输出结果: col1 object col2 object dtype: object ----------- col1 object col2 int32 dtype: object ----------- col1 object col2 float64 dtype: object 1 2 3 4 5 6 7 8 9 10 11 注:data type list Data type Description bool_ Boolean (True or False) stored as a byte int_ Default integer type (same as C long; normally either int64 or int32) intc Identical to C int (normally int32 or int64) intp Integer used for indexing (same as C ssize_t; normally either int32 or int64) int8 Byte (-128 to 127) int16 Integer (-32768 to 32767) int32 Integer (-2147483648 to 2147483647) int64 Integer (-9223372036854775808 to 9223372036854775807) uint8 Unsigned integer (0 to 255) uint16 Unsigned integer (0 to 65535) uint32 Unsigned integer (0 to 4294967295) uint64 Unsigned integer (0 to 18446744073709551615) float_ Shorthand for float64. float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa complex_ Shorthand for complex128. complex64 Complex number, represented by two 32-bit floats (real and imaginary components) complex128 Complex number, represented by two 64-bit floats (real and imaginary components) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 转自:http://blog.csdn.net/chinacmt/article/details/52230339

优秀的个人博客,低调大师

Python——Web.py詳解

ubuntu安裝Web.py 1 sudo pip install web.py 測試代碼: 1 import web 2 3 urls = ( 4 '/(.*)','hello' 5 ) 6 app = web.application(urls,globals()) 7 8 class hello: 9 def GET(self,name): 10 if not name: 11 name = 'world' 12 return 'hello,'+name+'!' 13 14 if __name__=='__main__': 15 app.run() 类似于flask模板,这里也可以使用文件读写返回html内容: 1 import web 2 3 urls = ( 4 '/(.*)','hello' 5 ) 6 app = web.application(urls,globals()) 7 8 class hello: 9 def GET(self,name): 10 return open(r'index.html','r').read() 11 12 if __name__=='__main__': 13 app.run() URL映射: 完全匹配: /index 模糊匹配 /post/\d+ 带组匹配 /post/(\d+) 1 import web 2 3 urls = ( 4 '/blog/\d+','blog', 5 '/index','index', 6 '/(.*)','hello', 7 ) 8 app = web.application(urls,globals()) 9 10 class index: 11 def GET(self): 12 return 'index' 13 14 class blog: 15 def GET(self): 16 return 'blog' 17 18 class hello: 19 def GET(self,name): 20 return 'hello' 21 22 if __name__=='__main__': 23 app.run() 请求处理: 请求参数获取 : web.input() hello.html源码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>User Login</title> 7 </head> 8 9 <body> 10 <div> 11 <h6>User Login</h6> 12 </div> 13 <form action="/blog/123" method="POST"> 14 <h6>用戶名:</h6> 15 <input type="text" name="username"><br> 16 <h6>密碼:</h6> 17 <input type="password" name="password"><br> 18 <input type="submit" name="submit"><br> 19 </form> 20 </body> 21 </html> app.py源码: 1 import web 2 3 urls = ( 4 '/blog/\d+','blog', 5 '/index','index', 6 '/(.*)','hello', 7 ) 8 app = web.application(urls,globals()) 9 10 class index: 11 def GET(self): 12 r = web.input() 13 return r 14 15 class blog: 16 def POST(self): 17 r = web.input() 18 return r 19 20 class hello: 21 def GET(self,name): 22 return open(r'hello.html','r').read() 23 24 if __name__=='__main__': 25 app.run() 运行结果: 请求头获取 : web.ctx.env app.py代码如下,hello.html代码如上相同 1 import web 2 3 urls = ( 4 '/blog/\d+','blog', 5 '/index','index', 6 '/(.*)','hello', 7 ) 8 app = web.application(urls,globals()) 9 10 class index: 11 def GET(self): 12 return web.ctx.env 13 14 class blog: 15 def POST(self): 16 r = web.input() 17 return r 18 19 class hello: 20 def GET(self,name): 21 return open(r'hello.html','r').read() 22 23 if __name__=='__main__': 24 app.run() 运行结果: 响应处理: 模板文件读取:render.index("参数") 在py文件同目录下要创建templates文件夹,存放模板。 index是指定模板的名称,参数是html中是所需参数。 1 import web 2 3 render = web.template.render("templates") 4 5 urls = ( 6 '/blog/\d+','blog', 7 '/index','index', 8 '/(.*)','hello', 9 ) 10 app = web.application(urls,globals()) 11 12 class hello: 13 def GET(self,name): 14 return render.hello() 15 16 if __name__=='__main__': 17 app.run() 采用模板就可以不用之前所用的open(r'hello.html','r').read()了。 结果数据获取:model.select("sql") 参考我之前的文章http://www.cnblogs.com/LexMoon/p/Flask_6.html URL跳转 :web.seeother("/") 1 import web 2 3 urls = ( 4 '/(.*)','hello', 5 ) 6 app = web.application(urls,globals()) 7 8 class hello: 9 def GET(self,name): 10 return web.seeother('http://www.cnblogs.com/LexMoon/') 11 12 if __name__=='__main__': 13 app.run()

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码