YOLO Logical Judgment Notes

1.cv2.VideoCapture() read url video

url = r'rtsp://xxx/vau/play/xxx?token=xxx'
cap = cv2.VideoCapture(url)
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
    # Display the resulting frame
    cv2.imshow('input', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    # When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

2.cv2.VideoCapture() read how to save video

fps, w, h = 30, im.shape[1], im.shape[0]   # 帧率/图片宽/图片高
save_path_video = str(Path(str(save_path + '/0')).with_suffix('.mp4'))  # force *.mp4 suffix on results videos   C:/……/0.mp4
vid_writer[j] = cv2.VideoWriter(save_path_video, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))   # 保存视频的对象
vid_writer[j].write(im)   # 保存每帧图片
vid_writer[j].release()  # release previous video writer

3. Save the dictionary as a json file and continue writing

with open(str(output_dir + '/data_test.json'), "r", encoding="utf-8") as f:   # 打开.json文件
    file = f.read()   # 读取文本
    if len(file) > 0:    # 如果文本不为空
        old_data = json.loads(file)   # 将Json字符串解码成python对象
    else:
        old_data = {MessageWarn_dict['Event_ID']: MessageWarn_dict}   # 添加信息
    old_data[MessageWarn_dict['Event_ID']] = MessageWarn_dict   # 继续添加信息
    print('old_data1:', old_data)

with open(str(output_dir + '/data_test.json'),'w',encoding="utf-8") as json_file:   # 写入.json文件
    json_MessageWarn_dict = json.dumps(old_data,ensure_ascii=False,indent=4,)   #将python对象编码成Json字符串 ensure_ascii=True:默认输出ASCLL码,如果把这个该成False,就可以输出中文。
    json_file.write(json_MessageWarn_dict)   # 字典写入json

4.logging save log

import logging
from logging.handlers import RotatingFileHandler

# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)

#定义一个RotatingFileHandler,最多备份3个日志文件,每个日志文件最大1K
rHandler = RotatingFileHandler("log.txt",maxBytes = 1*1024,backupCount = 3)
rHandler.setLevel(logging.INFO)
#创建一个FileHandler,并对输出消息的格式进行设置,将其添加到logger,然后将日志写入到指定的文件中
handler = logging.FileHandler("log.txt",encoding="utf-8",mode="a")   # FileHandler日志输出到文件,加上 encoding="utf-8",mode="a" 完美解决中文乱码的问题
handler.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
rHandler.setFormatter(formatter)

console = logging.StreamHandler()   # StreamHandler日志输出到流,可以是sys.stderr,sys.stdout或者文件
console.setLevel(logging.INFO)

logger.addHandler(handler)
logger.addHandler(console)


logger.info("Start print log")
logger.debug("Do something")
logger.warning("Something maybe fail.")
logger.info("Finish")

5.requests reptile novel

import requests
from bs4 import BeautifulSoup
import re
def pachong():
	response = requests.get("https://www.hsrnsw.org/xshs/111936/")
	soup = BeautifulSoup(response.content, 'lxml')
	soup_catalog = soup.select('div.box_con dl dd a')
	for i in soup_catalog:
		url = "https://www.hsrnsw.org" + str(re.findall(r'"(.+?)"',str(i))[0])
		print(url)
		response = requests.get(url)
		# print(response.text)
		soup = BeautifulSoup(response.content,'lxml')
		soup_catalog = soup.select('div.box_con div')[1].text
		print('111',soup_catalog.split('\n')[1],'111',type(soup_catalog))
		content = soup.find("div", id="content")
		c = str(content).split('\n')[1].replace('<br/><br/>','\n').strip('</div>')
		text = soup_catalog.split('\n')[1] + '\n' + c
		with open(r"F:\牛法\新建文件夹\卿是朝朝暮暮.txt", 'a+') as f:
			f.write(text)

Guess you like

Origin blog.csdn.net/m0_64118152/article/details/130265308