循环列表
1 2 3
| import itertools
l = itertools.cycle([1,])
|
conda 安装
conda环境配置,先运行 conda config --set show_channel_urls yes
,然后修改 .condarc文件
,最后运行 conda clean -i
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64 - defaults show_channel_urls: true default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
remote_read_timeout_secs: 1000.0 ssl_verify: false
|
conda命令
conda create -n xxx python=3.9
conda env remove -n xxx
conda deactivate
conda init
pytorch 安装
查看设备:运行 dxdiag
,检查CPU、GPU
驱动版本和CUDA版本
先确定torch版本和CUDA版本
然后生成命令
1
| conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c nvidia
|
fake-useragent ==1.5.1 的使用
1
| from fake_useragent import UserAgent
|
1 2 3 4 5 6
| ua = UserAgent( browsers=['firefox', 'chrome', 'safari'], os=["android", "ios"], min_version=120.0, platforms=["mobile"] )
|
1 2 3 4 5 6
| ua = UserAgent( browsers=["firefox", "chrome", "safari"], os=["windows", "macos"], min_version=120.0, platforms=["pc"], )
|
两个列表的差值
1
| list(set(account_shop_lst).difference(set(data_shop_lst)))
|
re.sub 替换目标分组
1 2 3 4 5
| def _new_base_info(text: str, new_str: str): pattern = r'cookie":".*?"' ck_str = re.search(pattern, text).group(0) new_ck_str = re.sub(r':".*?"', f':"{new_str}"', ck_str) return re.sub(ck_str, new_ck_str, device.base_info)
|
万 - 转换数字
1 2
| if "万" in item["soldCount"]: item["soldCount"] = int(eval(item["soldCount"].replace("万", " * 10000")))
|
Python 项目结构
除了utils
和init
文件夹之外,还有许多其他常见的项目结构和文件夹
main.py
或 app.py
:
models/
:
views/
:
controllers/
或 controllers.py
:
- 用于MVC架构中的控制器层,处理用户输入和业务逻辑调用。
services/
:
repositories/
:
migrations/
:
tests/
:
config/
:
scripts/
:
static/
:
- 如果是Web应用,存放静态文件,如CSS、JavaScript和图片。
templates/
:
docs/
或 documentation/
:
requirements.txt
:
setup.py
:
README.md
:
- 项目的说明文件,通常包含项目描述、安装指南和使用方法。
.gitignore
:
.env
:
.pytest.ini
或 tox.ini
:
Dockerfile
:
通用缺口识别 captcha-recognizer
https://github.com/chenwei-zhao/captcha-recognizer
1 2 3 4 5
| from captcha_recognizer.recognizer import Recognizer
recognizer = Recognizer() box, confidence = recognizer.identify_gap(source=img_name, verbose=False)
|
需要 Python 3.8 +
以及 torch 1.8 +
以上环境
ddddocr的使用
目标检测(det模式)
1 2 3 4 5
| ocr = ddddocr.DdddOcr(det=True, show_ad=False) image_file = "big.png" with open(image_file, "rb") as f: image = f.read() poses = ocr.detection(image)
|
画框
1 2 3 4 5
| im = cv2.imread(image_file) for box in poses: x1, y1, x2, y2 = box im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2) cv2.imwrite("result_" + image_file, im)
|
裁剪
1 2 3 4
| img = Image.open("big.png") for num, box in enumerate(poses): cap = img.crop(box) cap.save(f"{num}.png")
|
识别字符
1 2 3 4 5 6
| ocr = ddddocr.DdddOcr(show_ad=False) for num in range(5): num_img = f"{num}.png" _bytes = open(num_img, "rb").read() result = ocr.classification(_bytes) print(num_img, result)
|
识别缺口
第一种
1 2 3 4 5 6 7 8 9
| def get_distance_by_slide(small_name, big_name): """ 比较滑块和缺口图 """ slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False) target_bytes = open(small_name, "rb").read() background_bytes = open(big_name, "rb").read() res = slide.slide_match(target_bytes, background_bytes, simple_target=True) return res["target"][0]
|
第二种
1 2 3 4 5 6 7 8 9
| def get_distance_by_two_img(full_img_name, cut_img_name): """ 比较完整图和缺口图 """ slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False) target_bytes = open(cut_img_name, "rb").read() background_bytes = open(full_img_name, "rb").read() res = slide.slide_comparison(target_bytes, background_bytes) return res["target"][0]
|
选出长度较短的列表
1
| min_lst = min(ori_lst, des_lst, key=len)
|
js文件导入模块
1 2 3
| const b = require('./b.js'); b.encrypt("your data");
|
b.js
需要将模块主动导出,有两种方式
1 2 3 4 5 6 7 8
| function encrypt(data) { }
module.exports = { encrypt: encrypt };
|
1 2 3 4
| exports.encrypt = function(data) { };
|