- *********************************************************************************************************
- *相機獲取圖片
- *open_framegrabber ('USB3Vision', 0, 0, 0, 0, 0, 0, 'progressive', -1, 'default', -1, 'false', 'default', '2BDF59167987_Hikrobot_MVCE12010UM', 0, -1, AcqHandle)
- *grab_image_start (AcqHandle, -1)
- *grab_image_async (Image, AcqHandle, -1)
- *讀取固定圖片
- read_image(Image, 'D:/DLtest/testImages/OK/MSIK_0823_165614.bmp')
- get_image_size(Image, Width, Height)
- dev_open_window(0, 0, Width/4, Height/4, 'black', WindowHandle)
- dev_display(Image)
- **************************************************
- *讀取訓練模型
- *理論基礎模型的成功率只有89%,但是實際上239張圖片有10個是錯誤判定,成功率高達96%
- *實際成功率比理論高,這個模型是否可以使用
- read_dl_model('C:/Users/yifanzhao/Desktop/螺絲訓練/108.hdl',DLModelHandle)
- *設置硬件參數
- query_available_dl_devices (['runtime', 'runtime', 'id'], ['gpu', 'cpu', 0], DLDeviceHandle)
- set_dl_model_param (DLModelHandle, 'device', DLDeviceHandle[0])
- *設置預處理參數
- create_dl_preprocess_param_from_model (DLModelHandle, 'none', 'full_domain', [], [], [], DLPreprocessParam)
- *獲取模型參數
- get_dl_model_param (DLModelHandle, 'meta_data', MetaData)
- *異常值分類閾值
- InferenceClassificationThreshold := number(MetaData.anomaly_classification_threshold)
- *閾值下限
- InferenceSegmentationThreshold := number(MetaData.anomaly_segmentation_threshold)
- *分類字典
- DLDatasetInfo := dict{class_names: ['OK', 'NG'], class_ids: [0, 1]}
- *窗體字典
- WindowDict := dict{}
- **************************************************
- *排綫
- * read_dl_model('C:/Users/yifanzhao/Desktop/螺絲訓練/排綫模型.hdl',DLModelHandlePX)
- * query_available_dl_devices(['runtime', 'runtime', 'id'], ['gpu', 'cpu', 0], DLDeviceHandles)
- * set_dl_model_param(DLModelHandlePX, 'device', DLModelHandlePX[0])
- * create_dl_preprocess_param_from_model(DLModelHandlePX, 'none', 'full_domain', [], [], [], DLPreprocessParamPX)
- * get_dl_model_param(DLModelHandlePX, 'meta_data', MetaDataPX)
- * InferenceClassificationThreshold:=number(MetaDataPX.anomaly_classification_threshold)
- * InferenceSegmentationThreshold := number(MetaDataPX.anomaly_segmentation_threshold)
- * WindowDict:=dict{}
- * PXOK:=0
- * PXNG:=0
- **************************************************
- *電腦選取
- draw_rectangle2(WindowHandle, ComputerRow, ComputerColumn, ComputerPhi, ComputerLength1, ComputerLength2)
- gen_rectangle2(ComputerRectangle, ComputerRow, ComputerColumn, ComputerPhi, ComputerLength1, ComputerLength2)
- reduce_domain(Image, ComputerRectangle, ImageComputer)
- create_shape_model(ImageComputer, 'auto', 0, rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
- *標志選取
- draw_rectangle2(WindowHandle, SignRow, SignColumn, SignPhi, SignLength1, SignLength2)
- gen_rectangle2(SignRectangle, SignRow, SignColumn, SignPhi, SignLength1, SignLength2)
- reduce_domain(Image, SignRectangle, ImageSign)
- inspect_shape_model(ImageSign, ModelImages, ModelRegions, 4, 30)
- create_shape_model(ImageSign, 'auto', 0, rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID1)
- get_shape_model_contours(ModelContours, ModelID1, 1)
- area_center(SignRectangle, Area, Row, Column)
- *roi選取
- gen_empty_obj(RegionRoi)
- for Index := 1 to 30 by 1
- draw_circle(WindowHandle, circleRow, circleColumn, Radius)
- gen_circle(Circle, circleRow, circleColumn, 23)
- concat_obj(RegionRoi, Circle, RegionRoi)
- endfor
- *排綫選取
- * gen_empty_obj(PXRoi)
- * for IndexPX := 1 to 2 by 1
- * draw_rectangle2(WindowHandle, Row3, Column3, Phi, Length1, Length2)
- * gen_rectangle2(Rectangle, Row3, Column3, Phi, Length1, Length2)
- * concat_obj(PXRoi, Rectangle, PXRoi)
- * endfor
- *OK與NG標志
- resultOK:=0
- resultNG:=0
- *獲取時間
- get_system_time (MSecond, Second, Minute, Hour, Day, YDay, Month, Year)
- tuple_string(Month, '.2d', Month_String)
- tuple_string(Day, '.2d', Day_String)
- tuple_string(Hour, '.2d', Hour_String)
- tuple_string(Minute, '.2d', Minute_String)
- tuple_string(Second, '.2d', Second_String)
- name := 'MSIK_'+Month_String + Day_String + '_'+ Hour_String + Minute_String + Second_String+'.bmp'
- *循環驗證
- *循環驗證所有螺絲,將每一個位置的螺絲截取為圖片,然後與模型進行對比
- *但是DLResult中的數據提取不出來,提取出來就可進行對比。
- *是否可以直接將數據拿出或者只是調動數據進行對比。
- list_files ('D:/DLtest/testImages/OK', ['files','follow_links'], ImageFiles)
- tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
- for IndexInference := 0 to |ImageFiles| - 1 by 1
- read_image (TestImage, ImageFiles[IndexInference])
- *相機獲取圖片
- *grab_image(TestImage, AcqHandle)
- *讀取固定圖片
- *read_image(TestImage, 'D:/DLtest/testImages/OK/MSIK_0823_165614.bmp')
- *尋找電腦
- find_shape_model(TestImage, ModelID, 0, rad(360), 0.3, 1, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle, ComputerScore)
- if(|ComputerScore|>0)
- *尋找標志物
- find_shape_model(TestImage, ModelID1, 0, rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle1, SignScore)
- if(|SignScore|>0)
- *仿射變換匹配標志物位置
- vector_angle_to_rigid(0,0,0, Row2, Column2, Angle1, SignHomMat2D)
- affine_trans_contour_xld(ModelContours, ContoursAffineTrans, SignHomMat2D)
- *仿射變換匹配螺絲ROI位置
- vector_angle_to_rigid(Row, Column, 0, Row2, Column2, Angle1, RoiHomMat2D)
- affine_trans_region(Circle, RegionAffineTrans, RoiHomMat2D, 'nearest_neighbor')
- *仿射變換匹配排綫位置
- *vector_angle_to_rigid(0,0,0, Row2, Column2, Angle1, PXHomMat2D)
- *affine_trans_region(PXRoi, RegionAffineTransPX, PXHomMat2D, 'nearest_neighbor')
- endif
-
- dev_display(TestImage)
- *dev_close_window()
- *循環檢測
- *排綫
- *for PXIndex := 1 to 2 by 1
- * select_obj(PXRoi, ObjectSelectedPX, PXIndex)
- * reduce_domain(TestImage, ObjectSelectedPX, CheckImage)
-
- *endfor
- *螺絲
- gen_empty_obj(ErrRegion)
- for TIndex := 1 to 30 by 1
- *搜索螺絲集合的螺絲圖片
- select_obj(RegionAffineTrans, ObjectSelected, TIndex)
- reduce_domain(TestImage, ObjectSelected, CheckImage)
- crop_domain(CheckImage, ImagePart)
- *dev_clear_window()
- *dev_display(CheckImage)
- *圖片與模型檢測
- gen_dl_samples_from_images (ImagePart, DLSample)
- preprocess_dl_samples (DLSample, DLPreprocessParam)
- *分類檢測
- apply_dl_model (DLModelHandle, DLSample, [], DLResult)
- *判斷閾值
- threshold_dl_anomaly_results (InferenceSegmentationThreshold, InferenceClassificationThreshold, DLResult)
- *顯示結果
- *判斷結果並統計,查看DLResult具體的值并進行分類*********************
- dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, ['anomaly_result', 'anomaly_image'], [], WindowDict)
- get_dict_param(DLResult, 'keys', [], GenParamValue)
- get_dict_tuple(DLResult, 'anomaly_class_id', Tuple)
- if(Tuple=0)
- resultOK:=resultOK+1
- else
- resultNG:=resultNG+1
- concat_obj(ErrRegion, ImagePart, ErrRegion)
- endif
- dev_disp_text ('Press F5 (continue)', 'window', 'bottom', 'center', 'black', [], [])
- *stop()
-
- endfor
- * dev_display(TestImage)
- * dev_set_color('green')
- * if(resultNG>1)
- * dev_set_color('red')
- * dev_display(ErrRegion)
- * set_display_font (WindowHandle, 30, 'mono', 'true', 'true')
- * disp_message (WindowHandle, 'NG', 'window', 12, 12, 'red', 'false')
- * wait_seconds(1)
- * else
- * set_display_font (WindowHandle, 30, 'mono', 'true', 'true')
- * disp_message (WindowHandle, 'OK', 'window', 12, 12, 'green', 'false')
- * wait_seconds(1)
- * endif
- stop()
- endif
- endfor
- * while(true)
-
- * endwhile
复制代码 |