{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport cv2\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:17:20.355570Z","iopub.execute_input":"2023-03-23T03:17:20.357274Z","iopub.status.idle":"2023-03-23T03:17:20.366137Z","shell.execute_reply.started":"2023-03-23T03:17:20.357168Z","shell.execute_reply":"2023-03-23T03:17:20.364004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def identifica_cor_texto(caminho):\n    # Ler a imagem\n    imagem = cv2.imread(caminho)\n\n    # Converter a imagem para escala de cinza\n    imagem_cinza = cv2.cvtColor(imagem, cv2.COLOR_BGR2GRAY)\n\n    # Aplicar um desfoque para reduzir o ruído na imagem\n    imagem_suavizada = cv2.GaussianBlur(imagem_cinza, (5, 5), 0)\n\n    # Limiarização binária invertida\n    _, imagem_binaria = cv2.threshold(imagem_suavizada, 128, 255, cv2.THRESH_BINARY_INV)\n\n    # Operação morfológica de fechamento para conectar elementos próximos\n    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))\n    imagem_fechada = cv2.morphologyEx(imagem_binaria, cv2.MORPH_CLOSE, kernel)\n\n    # Remover ruído de pequenas áreas\n    contornos, _ = cv2.findContours(imagem_fechada, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    min_area = 50\n    for cnt in contornos:\n        if cv2.contourArea(cnt) < min_area:\n            cv2.drawContours(imagem_fechada, [cnt], -1, 0, -1)\n\n    # Calcular a intensidade média dos pixels\n    intensidade_media = imagem_fechada.mean()\n\n    if intensidade_media > 128:\n        return('branco')\n    else:\n        return('preto')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:17:20.369153Z","iopub.execute_input":"2023-03-23T03:17:20.369722Z","iopub.status.idle":"2023-03-23T03:17:20.381452Z","shell.execute_reply.started":"2023-03-23T03:17:20.369681Z","shell.execute_reply":"2023-03-23T03:17:20.379897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define o caminho onde está localizado as imagens para treinamento\ncaminho_kaggle = '/kaggle/input/benetech-making-graphs-accessible/train/images/'","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:17:20.383673Z","iopub.execute_input":"2023-03-23T03:17:20.384125Z","iopub.status.idle":"2023-03-23T03:17:20.398921Z","shell.execute_reply.started":"2023-03-23T03:17:20.384069Z","shell.execute_reply":"2023-03-23T03:17:20.397414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lista o nome dos arquivos e cria um dataframe\nlista_imagens = os.listdir(caminho_kaggle)\ndf_imagens = pd.DataFrame(lista_imagens, columns=['Nome'])","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:17:20.401434Z","iopub.execute_input":"2023-03-23T03:17:20.401865Z","iopub.status.idle":"2023-03-23T03:17:20.443848Z","shell.execute_reply.started":"2023-03-23T03:17:20.401823Z","shell.execute_reply":"2023-03-23T03:17:20.442252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Realiza um looping idenficando entre \"branco\" e \"preto\" a cor predominante do textos e números\nfor i in range(len(df_imagens)):\n    df_imagens.loc[i, 'Texto_Cor'] = identifica_cor_texto(caminho_kaggle + df_imagens.loc[i]['Nome'])\n    \n    # Estou definindo um limite somente para testar algumas imagens\n    if i > 1000:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:22:04.832864Z","iopub.execute_input":"2023-03-23T03:22:04.833372Z","iopub.status.idle":"2023-03-23T03:22:10.642872Z","shell.execute_reply.started":"2023-03-23T03:22:04.833329Z","shell.execute_reply":"2023-03-23T03:22:10.641364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Através da cor do texto estou considerando que o fundo é o inverso.\ndf_imagens.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:22:10.644823Z","iopub.execute_input":"2023-03-23T03:22:10.645594Z","iopub.status.idle":"2023-03-23T03:22:10.660837Z","shell.execute_reply.started":"2023-03-23T03:22:10.645545Z","shell.execute_reply":"2023-03-23T03:22:10.659419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_imagens[df_imagens['Texto_Cor'] == 'branco'].head()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:22:49.353895Z","iopub.execute_input":"2023-03-23T03:22:49.354600Z","iopub.status.idle":"2023-03-23T03:22:49.375825Z","shell.execute_reply.started":"2023-03-23T03:22:49.354525Z","shell.execute_reply":"2023-03-23T03:22:49.374552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}