出现ValueError

from pgmpy.models import BayesianModel
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination

# 通过边来定义贝叶斯模型
model = BayesianModel([('D', 'G'), ('I', 'G'), ('G', 'L'), ('I', 'S')])

# 定义条件概率分布
cpd_d = TabularCPD(variable='D', variable_card=2, values=[[0.6, 0.4]])
cpd_i = TabularCPD(variable='I', variable_card=2, values=[[0.7, 0.3]])

# variable:变量
# variable_card:基数
# values:变量值
# evidence:
cpd_g = TabularCPD(variable='G', variable_card=3,
                   values=[[0.3, 0.05, 0.9,  0.5],
                           [0.4, 0.25, 0.08, 0.3],
                           [0.3, 0.7,  0.02, 0.2]],
                  evidence=['I', 'D'],
                  evidence_card=[2, 2])

cpd_l = TabularCPD(variable='L', variable_card=2,
                   values=[[0.1, 0.4, 0.99],
                           [0.9, 0.6, 0.01]],
                   evidence=['G'],
                   evidence_card=[3])

cpd_s = TabularCPD(variable='S', variable_card=2,
                   values=[[0.95, 0.2],
                           [0.05, 0.8]],
                   evidence=['I'],
                   evidence_card=[2])

# 将有向无环图与条件概率分布表关联
model.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)

# 验证模型:检查网络结构和CPD,并验证CPD是否正确定义和总和为1
model.check_model()
# 获得G点的概率表
print(model.get_cpds('G'))
# 获得G点的基数
print(model.get_cardinality('G'))
# 获取贝叶斯网络的局部依赖关系
print(model.local_independencies(['D','I','S','G','L']))
# 贝叶斯推理:变量消除
infer = VariableElimination(model)
print(infer.query(['G']), ['G'])
print(infer.query(['G'], evidence={'D':0, 'I':1})['G'])
print(infer.map_query('G'))

ValueError: values must be of shape (2, 1). Got shape: (1, 2)

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答

本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。

​​​​因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。

HALCON算子错误
当在函数 'main' 中调用 'gen_circle_contour_xld'时.line: 16.

Wrong number of values of control parameter 1 (HALCON错误代码: 1401)

代码如下
read_image (Image, 'C:/Users/zhang/Desktop/屏幕截图 2021-09-21 232135.png')
*read_image (Image, 'C:/Users/zhang/Desktop/20180831114255361.png') 这是原图,换了一张图后出现了错误
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display (Image)
*均值滤波
mean_image (Image, ImageMean, 3, 3)
*灰度范围,结果为掩码矩形内灰度最大值减最小值
gray_range_rect (ImageMean, ImageResult, 8, 8)
threshold (ImageResult, Regions, 40, 138)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 31134.7, 50000)
*生成区域的轮廓
gen_contour_region_xld (SelectedRegions, Contours, 'border')
fit_circle_contour_xld (Contours, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1)
gen_circle (Circle, Row, Column, Radius-60)
gen_circle (Circle1, Row, Column, Radius-5)
difference (Circle1, Circle, RegionDifference)

dev_open_window (0, 0, 900, 60, 'black', WindowHandle1)
*直角坐标转极坐标
polar_trans_image_ext (Image, PolarTransImage, Row, Column, 0, 6.28319, Radius-60, Radius-5, 900, 60, 'bilinear')
rotate_image (PolarTransImage, ImageRotate, 180, 'constant')

*以下是通过助手实现光学字符识别并自动生成的代码
create_text_model_reader ('manual', [], TextModel)
set_text_model_param (TextModel, 'char_width', 17)
set_text_model_param (TextModel, 'char_height', 26)
set_text_model_param (TextModel, 'stroke_width', 5.9)
set_text_model_param (TextModel, 'return_punctuation', 'false')
set_text_model_param (TextModel, 'uppercase_only', 'true')
set_text_model_param (TextModel, 'fragment_size_min', 17)
set_text_model_param (TextModel, 'eliminate_border_blobs', 'true')
set_text_model_param (TextModel, 'base_line_tolerance', 0.2)
set_text_model_param (TextModel, 'max_line_num', 1)

read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OcrHandle)

gen_rectangle1 (ROI_OCR_01_0, 2.77273, 86.9906, 31.1364, 290.457)

  • OCR 01:
  • OCR 01: Extract symbol regions (segmentation step)
  • OCR 01: Only consider first channel for color images
    access_channel (ImageRotate, TmpObj_Mono, 1)
    reduce_domain (TmpObj_Mono, ROI_OCR_01_0, TmpObj_MonoReduced_OCR_01_0)

TmpCtrl_Orientation := 0

  • OCR 01: Corrected for text line orientation
    TmpCtrl_RangeMin := -0.174533
    TmpCtrl_RangeMax := 0.174533
    text_line_orientation (TmpObj_MonoReduced_OCR_01_0, TmpObj_MonoReduced_OCR_01_0, 26, TmpCtrl_Orientation+TmpCtrl_RangeMin, TmpCtrl_Orientation+TmpCtrl_RangeMax, TmpCtrl_Orientation)
  • OCR 01: Build rotation matrix
    hom_mat2d_identity (TmpCtrl_MatrixIdentity)
    hom_mat2d_rotate (TmpCtrl_MatrixIdentity, -TmpCtrl_Orientation, 0, 0, TmpCtrl_MatrixRotation)
  • OCR 01: Apply transformation to image and domain
    get_domain (TmpObj_MonoReduced_OCR_01_0, TmpObj_Domain)
    get_system ('clip_region', TmpCtrl_ClipRegion)
    set_system ('clip_region', 'false')
    dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 29)
    affine_trans_region (TmpObj_DomainExpanded, TmpObj_DomainTransformedRaw, TmpCtrl_MatrixRotation, 'true')
    smallest_rectangle1 (TmpObj_DomainTransformedRaw, TmpCtrl_Row1, TmpCtrl_Col1, TmpCtrl_Row2, TmpCtrl_Col2)
    hom_mat2d_translate (TmpCtrl_MatrixIdentity, -TmpCtrl_Row1, -TmpCtrl_Col1, TmpCtrl_MatrixTranslation)
    hom_mat2d_compose (TmpCtrl_MatrixTranslation, TmpCtrl_MatrixRotation, TmpCtrl_MatrixComposite)
    affine_trans_region (TmpObj_Domain, TmpObj_DomainTransformed, TmpCtrl_MatrixComposite, 'true')
    affine_trans_image (TmpObj_MonoReduced_OCR_01_0, TmpObj_ImageTransformed, TmpCtrl_MatrixComposite, 'constant', 'true')
    dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 29)
    expand_domain_gray (TmpObj_ImageTransformed, TmpObj_ImageTransformedExpanded, 29)
    reduce_domain (TmpObj_ImageTransformed, TmpObj_DomainTransformed, TmpObj_ImageTransformedReduced)
    crop_part (TmpObj_ImageTransformedReduced, TmpObj_MonoReduced_OCR_01_0, 0, 0, TmpCtrl_Col2-TmpCtrl_Col1+1, TmpCtrl_Row2-TmpCtrl_Row1+1)
    set_system ('clip_region', TmpCtrl_ClipRegion)
    find_text (TmpObj_MonoReduced_OCR_01_0, TextModel, TmpCtrl_ResultHandle_OCR_01_0)
  • OCR 01: Read text (classification step)
    get_text_object (Symbols_OCR_01_0, TmpCtrl_ResultHandle_OCR_01_0, 'all_lines')
    clear_text_result (TmpCtrl_ResultHandle_OCR_01_0)
    dev_display (TmpObj_MonoReduced_OCR_01_0)
    dev_set_draw ('fill')
    dev_set_colored (3)
    dev_display (Symbols_OCR_01_0)
    do_ocr_multi_class_mlp (Symbols_OCR_01_0, TmpObj_MonoReduced_OCR_01_0, OcrHandle, SymbolNames_OCR_01_0, Confidences_OCR_01_0)

dev_display (ImageRotate)
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
for i:=0 to |SymbolNames_OCR_01_0|-1 by 1
disp_message (WindowHandle1, SymbolNames_OCR_01_0[i], 'image', 30, 10*i+88, 'white', 'false')
endfor
clear_text_model (TextModel)
clear_ocr_class_mlp (OcrHandle)