{"id":796,"date":"2021-12-07T11:45:32","date_gmt":"2021-12-07T02:45:32","guid":{"rendered":"https:\/\/rfsec.ddns.net\/db\/?p=796"},"modified":"2021-12-07T11:45:34","modified_gmt":"2021-12-07T02:45:34","slug":"%e6%95%b0%e7%8b%ac%e3%81%ae%e5%95%8f%e9%a1%8c%e7%94%bb%e5%83%8f%e3%81%8b%e3%82%89%e6%95%b0%e5%ad%97%e3%82%92%e8%87%aa%e5%8b%95%e3%81%a7%e5%88%87%e3%82%8a%e5%87%ba%e3%81%99","status":"publish","type":"post","link":"https:\/\/rfsec.ddns.net\/db\/?p=796","title":{"rendered":"\u6570\u72ec\u306e\u554f\u984c\u753b\u50cf\u304b\u3089\u6570\u5b57\u3092\u81ea\u52d5\u3067\u5207\u308a\u51fa\u3059"},"content":{"rendered":"\n<p>\u8a8d\u8b58\u306b\u90aa\u9b54\u306a\u7dda\u3092cv2.HoughLinesP\u306e\u6a5f\u80fd\u3067\u5224\u5b9a\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u3059\u304c\u3001\u3053\u306e\u95a2\u6570\u3067\u306f\u3001\u7dda\u3068\u3057\u3066\u8a8d\u8b58\u3057\u305f\u5ea7\u6a19(x0,y0)-(x1,y1)\u306e\u30ea\u30b9\u30c8\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u5ea7\u6a19\u7fa4\u304b\u3089, Xmin,Xmax,Ymin,Ymax\u3092\u6c42\u3081\u308b\u3068\u3001\u554f\u984c\u30de\u30b9\u306e\u5de6\u4e0a\u3068\u53f3\u4e0b\u306e\u5ea7\u6a19\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u7dda\u3092\u8a8d\u8b58\u3059\u308b\u969b\u306b\u3001\u5c11\u306a\u304f\u3066\u3082\u5de6\u7aef\u3068\u53f3\u7aef\u306e\u5782\u76f4\u7dda\u3001\u6c34\u5e73\u7dda\u306e\u4e0a\u7aef\u3001\u4e0b\u7aef\u306e\u7dda\u3092\u8a8d\u8b58\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u304c\u6761\u4ef6\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u6b21\u306e\u753b\u50cf\u3067\u306f\u3001\u7dd1\u304c\u7dda\u3068\u3057\u3066\u8a8d\u8b58\u3057\u305f\u7b87\u6240\u3002\uff14\u500b\u306e\u9752\u3067\u56f2\u3093\u3060\u7dda\u304c\u3001\u4f4d\u7f6e\u306e\u5224\u5b9a\u306b\u5fc5\u8981\u306a\u7dda\u3067\u3059\u3002\uff14\u672c\u306e\u7dda\u306e\u5ea7\u6a19\u304b\u3089\u3001\u8d64\u4e38\u306e\u5ea7\u6a19\u3082\u7b97\u51fa\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u5ea7\u6a19\u304b\u3089\u3001\uff11\u6587\u5b57\u6bce\u306e\u753b\u50cf\u3092\u5207\u308a\u51fa\u3057\u3066\u500b\u3005\u306e\u753b\u50cf\u3092\u8a8d\u8b58\u3059\u308b\u3088\u3046\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"583\" height=\"585\" src=\"https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/12\/top-bot.png\" alt=\"\" class=\"wp-image-798\" srcset=\"https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/12\/top-bot.png 583w, https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/12\/top-bot-300x300.png 300w, https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/12\/top-bot-150x150.png 150w\" sizes=\"auto, (max-width: 583px) 100vw, 583px\" \/><\/figure>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import streamlit as st\nimport cv2\nfrom PIL import Image           # \u753b\u50cf\u51e6\u7406\u30e9\u30a4\u30d6\u30e9\u30ea\n#from matplotlib import pyplot as plt # \u30c7\u30fc\u30bf\u30d7\u30ed\u30c3\u30c8\u7528\u30e9\u30a4\u30d6\u30e9\u30ea\nimport numpy as np              # \u30c7\u30fc\u30bf\u5206\u6790\u7528\u30e9\u30a4\u30d6\u30e9\u30ea\n#import os                       # os \u306e\u60c5\u5831\u3092\u6271\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\nimport pytesseract              # tesseract \u306e python \u7528\u30e9\u30a4\u30d6\u30e9\u30ea\nimport unicodedata\n\ndef remove_control_characters(s):\n    return &quot;&quot;.join(ch for ch in s if unicodedata.category(ch)[0]!=&quot;C&quot;)\n\ndef erase_lines(img,img_thresh,th1):\n    # OpenCV\u3067\u76f4\u7dda\u306e\u691c\u51fa\n    # https:\/\/qiita.com\/tifa2chan\/items\/d2b6c476d9f527785414\n    img2 = img.copy()\n    img3 = img.copy()\n    gray = cv2.cvtColor(img_thresh, cv2.COLOR_BGR2GRAY)\n    gray_list = np.array(gray)\n    gray2 = cv2.bitwise_not(gray)\n    gray2_list = np.array(gray2)\n    lines = cv2.HoughLinesP(gray2, rho=1, theta=np.pi\/360, threshold=th1, minLineLength=150, maxLineGap=5)\n    xmin,ymin=500,500\n    xmax,ymax=0,0\n    if lines is not None:\n        for line in lines: # Xmin,Xmax,Ymin,Ymax\u306e\u7b97\u51fa\n            x1, y1, x2, y2 = line[0]\n            if x1&lt;xmin:\n                xmin=x1\n            if y1&lt;ymin:\n                ymin=y1\n            if x1&gt;xmax:\n                xmax=x1\n            if y1&gt;ymax:\n                ymax=y1\n\n            # \u7dd1\u8272\u306e\u7dda\u3092\u5f15\u304f\n            red_lines_img = cv2.line(img2, (x1,y1), (x2,y2), (0,255,0), 3)\n            red_lines_np=np.array( red_lines_img)\n            #cv2.imwrite(&quot;calendar_mod3.png&quot;, red_lines_img)\n\n            # \u7dda\u3092\u6d88\u3059(\u767d\u3067\u7dda\u3092\u5f15\u304f)\n            no_lines_img = cv2.line(img_thresh, (x1,y1), (x2,y2), (255,255,255), 3)\n            no_lines=np.array( no_lines_img)\n        dx=int(0.5+(xmax-xmin)\/9)\n        dy=int(0.5+(ymax-ymin)\/9)\n        sx=int(0.5+dx*0.2)\n        sy=int(0.5+dy*0.2)\n        st.write(xmin,ymin,xmax,ymax,dx,dy)\n        peaces=[]\n        for y in range(9):\n            for x in range(9):\n                p = xmin + x*dx + sx\n                q = ymin + y*dy + sy\n                cv2.rectangle(no_lines,(p,q),(p+dx-sx,q+dy-sy),(0,0,255),1)\n                peaces.append(cv2.cvtColor(no_lines_img[q:q+dy-sy,p:p+dx-sx],cv2.COLOR_BGR2RGB))\n                #st.image(peace,caption=str(x)+&#39;,&#39;+str(y))\n        im_h= cv2.hconcat([red_lines_img, img_thresh])\n    else:\n        im_h = None\n        no_lines = img_thresh\n    return im_h, no_lines,peaces\n\ndef main():\n    st.title(&#39;\u6587\u5b57\u8a8d\u8b58\u306e\u5b9f\u9a13&#39;)\n    col1, col2 ,col3, col4 = st.columns([3,1,1,1])\n    KEI = None\n    with col1:\n        uploaded_file = st.file_uploader(&quot;\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u9078\u629e\u3057\u3066\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9&quot;)\n    if uploaded_file is not None:\n        img = Image.open(uploaded_file)\n        img = np.array(img)\n        th2 = st.slider(label=&#39;\uff12\u5024\u5316\u306e\u95be\u5024&#39;,min_value=0, max_value=255, value=100)\n        th1 = st.slider(label=&#39;\u7dda\u6d88\u53bb\u306e\u95be\u5024&#39;,min_value=0, max_value=255, value=100)\n        with col2:\n            LNG =  st.selectbox(&quot;\u8a00\u8a9e\u9078\u629e&quot;,[&#39;eng&#39;,&#39;jpn&#39;])\n        with col3:\n            KEI = st.checkbox(&#39;\u7dda\u524a\u9664&#39;)\n        with col4:\n            OCR = st.checkbox(&#39;OCR\u5b9f\u884c&#39;)\n\n        ret, img_thresh = cv2.threshold(img, th2, 255, cv2.THRESH_BINARY)\n        im_h = cv2.hconcat([img, img_thresh])\n        st.image(im_h, caption=&#39;\u5143\u753b\u50cf&lt;---&gt;\uff12\u5024\u5316\u753b\u50cf&#39;)\n        if KEI:\n            im_h, no_lines, peaces = erase_lines(img,img_thresh,th1)\n            if im_h is None:\n                st.warning(&#39;No line detectd&#39;)\n            else:\n                new_image = cv2.cvtColor(im_h, cv2.COLOR_BGR2RGB)\n                st.image(new_image,caption=&#39;\u7dda\u3092\u524a\u9664\u3057\u305f\u753b\u50cf&#39;)\n        else:\n            no_lines=img_thresh\n\n        if OCR:\n            st.subheader(&#39;---\u8a8d\u8b58\u7d50\u679c---&#39;)\n            #txt = pytesseract.image_to_string(no_lines, lang=&quot;eng&quot;,config=&#39;--psm 11&#39;)\n            conf=&#39;-l &#39; + LNG + &#39; --psm 6  outputbase digits&#39;\n            n=0\n            row=&#39;&#39;\n            for peace in peaces:\n                txt=pytesseract.image_to_string(peace, config=conf)\n                txt=remove_control_characters(txt)\n                if txt.isdigit():\n                    ans=str(txt)+&#39; &#39;\n                else:\n                    ans=&#39;--&#39;\n                row=row+ans\n                n=n+1\n                if(n==9):\n                    st.write(row)\n                    row=&#39;&#39;\n                    n=0\nif __name__ == &#39;__main__&#39;:\n    main()<\/code><\/pre><\/div>\n\n\n\n<p><strong>\u8a8d\u8b58\u7d50\u679c\uff1a\uff11\uff10\uff10\uff05\u306e\u6b63\u89e3<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>1 --------6 8 ----\n5 ------------3 --\n------7 2 --------\n9 4 2 ----3 7 --6\n7 --3 ----4 1 ----\n8 --5 ----9 ------\n4 7 6 3 9 2 5 8 1\n2 5 1 --8 7 3 6 --\n--9 8 1 6 5 4 2 --<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8a8d\u8b58\u306b\u90aa\u9b54\u306a\u7dda\u3092cv2.HoughLinesP\u306e\u6a5f\u80fd\u3067\u5224\u5b9a\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u3059\u304c\u3001\u3053\u306e\u95a2\u6570\u3067\u306f\u3001\u7dda\u3068\u3057\u3066\u8a8d\u8b58\u3057\u305f\u5ea7\u6a19(x0,y0)-(x1,y1)\u306e\u30ea\u30b9\u30c8\u3092\u8fd4\u3057\u307e\u3059\u3002 \u3053\u306e\u5ea7\u6a19\u7fa4\u304b\u3089, Xmin,Xmax,Ymin,Ymax [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-796","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"featured_image_src":null,"author_info":{"display_name":"mars","author_link":"https:\/\/rfsec.ddns.net\/db\/?author=1"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=796"}],"version-history":[{"count":2,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":804,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions\/804"}],"wp:attachment":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}