<jupyter_start><jupyter_text>GitHub  https://github.com/clovaai/CRAFT-pytorch  論文  https://arxiv.org/abs/1904.01941        GitHubからCRAFTのソースコードを取得<jupyter_code>%cd /content/
!git clone https://github.com/clovaai/CRAFT-pytorch.git<jupyter_output><empty_output><jupyter_text>ライブラリのインストール<jupyter_code>%cd /content/CRAFT-pytorch/
!pip uninstall torch torchtext torchaudio -y
!pip install -r requirements.txt<jupyter_output><empty_output><jupyter_text>ライブラリのインポート<jupyter_code>import os
import gdown
import glob
from PIL import Image
import matplotlib.pyplot as plt
import random
import shutil

import torch<jupyter_output><empty_output><jupyter_text>学習済みモデルのダウンロード<jupyter_code>%cd /content/CRAFT-pytorch/
!mkdir pretrained
%cd /content/CRAFT-pytorch/pretrained

for google_drive_file_id in ['1Jk4eGD7crsqCCg9C9VjCLkMN3ze8kutZ']:
  gdown.download(
      url='https://drive.google.com/uc?id={}'.format(google_drive_file_id),
      output="./craft_mlt_25k.pth", 
      quiet=False
      )<jupyter_output><empty_output><jupyter_text>データセットのダウンロード<jupyter_code>%cd /content/CRAFT-pytorch/
!mkdir dataset
%cd /content/CRAFT-pytorch/dataset

for google_drive_file_id in ['1sptDnAomQHFVZbjvnWt2uBvyeJ-gEl-A']:
  gdown.download(
      url='https://drive.google.com/uc?id={}'.format(google_drive_file_id),
      output="./icdar2013.zip", 
      quiet=False
      )

# ダウンロードしたzipファイルの解凍
# 解凍したファイルすべてが出力されることを避けるため > /dev/null で出力を捨てる
!unzip ./icdar2013.zip > /dev/null

# icdar2013 testファイルリストを取得
test_path = "/content/CRAFT-pytorch/dataset/icdar2013/test_images/"
test_path_list = glob.glob(test_path + "*.jpg")<jupyter_output><empty_output><jupyter_text>文字検出(Character Detection)<jupyter_code># 文字検出対象のファイル選定
target_path = "/content/CRAFT-pytorch/inputs/"
if os.path.exists(target_path):
  shutil.rmtree(target_path)
  os.makedirs(target_path, exist_ok=True)
else:
  os.makedirs(target_path, exist_ok=True)

# 文字検出用にランダムに5枚選択
target = random.sample(test_path_list, 5)

for file_path in target:
  shutil.copy(file_path, target_path)

target_path_list = glob.glob(target_path + "*.jpg")

# 文字検出対象画像の表示
for t_path in target_path_list:
  image = Image.open(t_path).convert("RGB")
  plt.figure(figsize=(6, 6))
  plt.imshow(image)
%cd /content/CRAFT-pytorch/

# GPUが使用可能であればTrue、不可であればFalse
device = torch.cuda.is_available()
test_img = test_path_list[0]

!python3 test.py \
  --trained_model=/content/CRAFT-pytorch/pretrained/craft_mlt_25k.pth \
  --test_folder=$target_path \
  --cuda=$device<jupyter_output><empty_output><jupyter_text>文字検出結果の表示<jupyter_code>results = glob.glob("/content/CRAFT-pytorch/result/*.jpg")
results = [f for f in results if not 'mask' in f ]

# 文字検出対象画像の表示
for result in results:
  image = Image.open(result).convert("RGB")
  plt.figure(figsize=(6, 6))
  plt.imshow(image)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12926136363636365, 'fraction_numerical': 0.016335227272727272, 'mean_word_length': 5.837378640776699, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '503430', 'license': 'MIT', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1275, 'n_words': 149, 'path': 'CRAFT_demo.ipynb', 'repo_name': 'kaz12tech/ai_demos', 'size': '2816'}