总爱用的代码

程序1:
功能:从一个文件夹下读取所有图片,裁剪一部分,存到另一文件夹

save_path='E:/lixueqian/2019/Surveillance_video/yangben/'
img_path='E:/lixueqian/2019/save_jpg/org/'

frames= os.listdir(img_path)
for frame_pa in frames:
    frame_pa_path=os.path.join(img_path,frame_pa)
    img = cv2.imread(frame_pa_path)
    #cv2.imshow('img',img)
    #cv2.waitKey(0)
    print(img.shape)
    cropped = img[440:740, 0:1920]  # 裁剪坐标为[y0:y1, x0:x1]
    save_pa=os.path.join(save_path,frame_pa)
    cv2.imwrite(save_pa, cropped)

程序2:
功能:从一个文件夹下读取txt,修改后存到另一文件夹

def convert(size, box):
    dw = 1./size[0]
    dh = 1./size[1]
    x = (box[0] + box[1])/2.0
    y = (box[2] + box[3])/2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)
def cut_txt(indir,outdir):
    os.chdir(indir)  # 改变当前工作目录到指定路径
    txt = os.listdir(indir)
    for every_txt in txt:
        with open(every_txt) as read_file:
            content= read_file.read()
            #content.rstrip(\n)
            content= content.replace('\n', '')
            a =content.split(',')
            a = list(map(int, a))
            #print(every_txt,a)
            w=1920
            #h=1080
            h=300
            #w=1634
            #h=919
            print(every_txt,[a[0],a[2],a[1]-440,a[3]-440])
            x, y, w, h = convert((w, h),[a[0],a[2],a[1]-440,a[3]-440])
            print('0',x,y,w,h)

            txt_save_path=os.path.join(outdir,every_txt)
            fp = open(txt_save_path, 'w')
            fp.write('0')
            fp.write(' '+str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + ' ')
            fp.close()

程序3:
功能:将视频保存为一帧一帧的图片

save_dir=r'E:\lixueqian\2019\save_jpg\process'
org_dir=r'E:\lixueqian\2019\save_jpg\org'
success,frame = videoCapture.read()
while success:
    frame_org=copy.deepcopy(frame)
    ....
    cv2.imwrite(os.path.join(save_dir,img_name),frame)
    cv2.imwrite(os.path.join(org_dir, img_name), frame_org)
    success, frame = videoCapture.read()

程序4:列表按索引排序

xiaolunkuo_list.sort(key=lambda i: i[1], reverse=False) #false是从小到大,

程序5:将样本分类

# -*- coding: utf-8 -*-
import os
import numpy as np
import random
def all_path(image):
    dirs = os.listdir(image)
    f1= open("train.txt", "w+")
    f2=open('test.txt','w+')
    num_train=0
    num_test=0
    for son_dir in dirs:
        image_path=os.path.join(image,son_dir)
        temp=image_path+'\n'
        low=1
        high=100
        number = random.randint(0,100)
        if number<91:
            num_train+=1
            f1.write(temp)
        else:
            num_test+=1
            f2.write(temp)
    f1.close()
    f2.close()
    print num_train
    print num_test

程序6:将视频resize

# -*- coding: utf-8 -*-
import cv2
#videoCapture= cv2.VideoCapture(r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video.mp4")
#cap = cv2.VideoCapture('r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video.mp4"')
#videoCapture= cv2.VideoCapture(r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video.mp4")
video_src = r"E:\lixueqian\2019\new_method\read_result\1test_set2\target1_1.mp4"
videoCapture= cv2.VideoCapture(video_src)
#fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps =videoCapture.get(cv2.CAP_PROP_FPS)
#size=(int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)/4),int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)/4))
size=(int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)/4),int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)/4))
#size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#videoWriter = cv2.VideoWriter(r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video2.mp4",fourcc, fps, size)
#videoWriter = cv2.VideoWriter(r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video2.mp4",cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)
videoWriter = cv2.VideoWriter(r"E:\lixueqian\2019\new_method\read_result\1test_set2\target1_6.avi", cv2.VideoWriter_fourcc('I','4','2','0'),fps, size)

success,frame = videoCapture.read()
fps_num=0
while success:
    if fps_num%6==0:
        [width,height,xx]=frame.shape
        frame3=cv2.resize(frame,(int(height/4), int(width/4)),  interpolation=cv2.INTER_CUBIC)
        cv2.imshow('lxq',frame3)
        cv2.waitKey(1)
        videoWriter.write(frame3)
    fps_num+=1
    success, frame = videoCapture.read()

程序7:将视频隔帧保存

# -*- coding: utf-8 -*-
import cv2
#videoCapture= cv2.VideoCapture(r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video.mp4")
#cap = cv2.VideoCapture('r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video.mp4"')
#videoCapture= cv2.VideoCapture(r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video.mp4")
video_src = r"E:\lixueqian\2019\new_method\read_result\1test_set2\target1_1.mp4"
videoCapture= cv2.VideoCapture(video_src)

fourcc = cv2.VideoWriter_fourcc(*'XVID')
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(r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video2.mp4",fourcc, fps, size)
#videoWriter = cv2.VideoWriter(r"E:\lixueqian\2019\new_method\powerai-counting-cars-master\powerai-counting-cars-master\data\training_video2.mp4",cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)
videoWriter = cv2.VideoWriter(r"E:\lixueqian\2019\new_method\read_result\1test_set2\target1_3.mp4",fourcc, fps, size)
fps_num=0
success,frame = videoCapture.read()
while success:
    if fps_num%3==0:
        videoWriter.write(frame)
    fps_num+=1
    success, frame = videoCapture.read()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值