下载地址:https://www.pan38.com/share.php?code=pvvmX 提取码:8888 【仅供学习参考】
该脚本包含完整的功能模块和错误处理机制,实现了应用打开、登录操作、任务序列执行等功能。使用时需要根据实际界面元素调整选择器参数。该OCR系统包含完整的图像预处理、文本区域检测和OCR识别流程,支持中英文混合识别
/**
* AutoJS综合演示脚本
* 功能包含:界面操作、数据处理、定时任务等
*/
// ========== 基础配置 ==========
auto.waitFor();
device.keepScreenOn(3600 * 1000); // 保持屏幕亮1小时
setScreenMetrics(1080, 2340); // 设置屏幕分辨率
// ========== 全局变量 ==========
const CONFIG = {
retryTimes: 3,
waitTimeout: 1500,
debugMode: true
};
// ========== 主功能模块 ==========
function main() {
log("脚本启动");
initRuntime();
// 执行任务序列
executeTaskSequence([
openApp,
loginOperation,
checkNotifications,
processMainPage,
cleanUp
]);
log("脚本执行完成");
}
// ========== 功能函数 ==========
function initRuntime() {
// 初始化运行时环境
if (!requestScreenCapture()) {
toast("请授予截图权限");
exit();
}
events.on("exit", () => {
device.cancelKeepingAwake();
});
}
function executeTaskSequence(tasks) {
// 顺序执行任务数组
tasks.forEach((task, index) => {
log("开始执行任务" + (index + 1));
let retry = 0;
while (retry < CONFIG.retryTimes) {
try {
if (task()) break;
} catch (e) {
log("任务异常:" + e);
}
retry++;
sleep(1000);
}
});
}
function openApp() {
// 打开目标应用
const appName = "目标应用";
if (!launchApp(appName)) {
log("打开应用失败");
return false;
}
sleep(CONFIG.waitTimeout);
return true;
}
function loginOperation() {
// 登录操作处理
const loginBtn = findElement("text", "登录");
if (loginBtn) {
click(loginBtn.bounds().centerX(), loginBtn.bounds().centerY());
sleep(2000);
// 输入账号密码
const account = findElement("id", "account_input");
const password = findElement("id", "password_input");
if (account && password) {
account.setText("test_user");
password.setText("123456");
sleep(500);
// 点击确认按钮
const confirm = findElement("text", "确认登录");
if (confirm) {
click(confirm.bounds().centerX(), confirm.bounds().centerY());
sleep(3000);
return true;
}
}
}
return false;
}
// ========== 工具函数 ==========
function findElement(strategy, value) {
// 增强的元素查找函数
let element = null;
switch (strategy) {
case "text":
element = text(value).findOne(CONFIG.waitTimeout);
break;
case "id":
element = id(value).findOne(CONFIG.waitTimeout);
break;
case "desc":
element = desc(value).findOne(CONFIG.waitTimeout);
break;
}
return element;
}
function click(x, y) {
// 增强的点击函数
if (typeof x === "number" && typeof y === "number") {
press(x, y, 50);
} else if (x && x.bounds) {
press(x.bounds().centerX(), x.bounds().centerY(), 50);
}
sleep(300 + Math.random() * 200); // 随机延迟
}
// ========== 日志系统 ==========
function log(message) {
if (CONFIG.debugMode) {
console.log(new Date().toLocaleString() + " - " + message);
}
}
// ========== 脚本入口 ==========
module.exports = main;
// 立即执行
main();
import cv2
import numpy as np
import pytesseract
from PIL import Image
from skimage import filters
class CustomOCR:
def __init__(self, config_path=None):
self.min_confidence = 0.7
self.preprocess_methods = {
'adaptive_thresh': self._adaptive_threshold,
'otsu': self._otsu_threshold,
'edge_preserve': self._edge_preserving_filter
}
def _adaptive_threshold(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
def _otsu_threshold(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
_, thresh = cv2.threshold(
blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return thresh
def _edge_preserving_filter(self, image):
return cv2.edgePreservingFilter(image, flags=1, sigma_s=60, sigma_r=0.4)
def preprocess_image(self, image, method='otsu'):
processor = self.preprocess_methods.get(method)
if not processor:
raise ValueError(f"Unsupported method: {method}")
return processor(image)
def detect_text_regions(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sobelx = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
_, binary = cv2.threshold(sobelx, 0, 255, cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 5))
closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(
closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return sorted(contours, key=cv2.contourArea, reverse=True)
def recognize_text(self, image, lang='eng+chi_sim'):
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
text_data = pytesseract.image_to_data(
image, lang=lang, output_type=pytesseract.Output.DICT,
config=custom_config)
results = []
for i in range(len(text_data['text'])):
if int(text_data['conf'][i]) > self.min_confidence * 100:
results.append({
'text': text_data['text'][i],
'confidence': float(text_data['conf'][i]) / 100,
'bbox': (
text_data['left'][i],
text_data['top'][i],
text_data['width'][i],
text_data['height'][i])
})
return results
def process_image(self, image_path):
image = cv2.imread(image_path)
processed = self.preprocess_image(image)
contours = self.detect_text_regions(processed)
final_text = []
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
roi = image[y:y+h, x:x+w]
text_results = self.recognize_text(roi)
final_text.extend(text_results)
return {
'original_image': image,
'processed_image': processed,
'text_results': final_text
}
from ocr_system import CustomOCR
import matplotlib.pyplot as plt
def visualize_results(result):
plt.figure(figsize=(12,8))
plt.subplot(1,2,1)
plt.imshow(cv2.cvtColor(result['original_image'], cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.subplot(1,2,2)
plt.imshow(result['processed_image'], cmap='gray')
plt.title('Processed Image')
for item in result['text_results']:
x,y,w,h = item['bbox']
plt.gca().add_patch(plt.Rectangle(
(x,y), w, h, fill=False, edgecolor='red', linewidth=2))
plt.text(x, y-5, f"{item['text']}({item['confidence']:.2f})",
color='red', fontsize=8)
plt.tight_layout()
plt.show()
if __name__ == '__main__':
ocr = CustomOCR()
result = ocr.process_image('test_document.jpg')
print("识别结果:", [item['text'] for item in result['text_results']])
visualize_results(result)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。