书写生字Ai评分小程序
from handwriting_analysis import StrokeAnalyzer
class WritingEvaluator:
def __init__(self):
self.analyzer = StrokeAnalyzer
def evaluate(self, image_path):
图像预处理
processed_img = self._preprocess(image_path)
笔画分析
stroke_data = self.analyzer.extract_strokes(processed_img)
多维评分
scores = {
'stroke_order': self._check_stroke_order(stroke_data),
'structure': self._analyze_structure(stroke_data),
'pressure': self._measure_pressure(stroke_data)
return self._generate_feedback(scores)
import numpy as np
class StrokeAnalyzer:
def extract_strokes(self, image):
使用OpenCV提取笔画轨迹
contours, _ = cv2.findContours(
image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
笔画时序重建
strokes = []
for cnt in sorted(contours, key=lambda x: x[0][0][0]):
strokes.append({
'points': [tuple(pt[0]) for pt in cnt],
'direction': self._calc_direction(cnt)
})
return strokes