---
title: HackMD Dark Theme
tags: theme
description: Use `{%hackmd theme-dark %}` syntax to include this theme.
---
<style>
html, body, .ui-content {
background-color: #333;
color: #ddd;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
color: #ddd;
}
.markdown-body h1,
.markdown-body h2 {
border-bottom-color: #ffffff69;
}
.markdown-body h1 .octicon-link,
.markdown-body h2 .octicon-link,
.markdown-body h3 .octicon-link,
.markdown-body h4 .octicon-link,
.markdown-body h5 .octicon-link,
.markdown-body h6 .octicon-link {
color: #fff;
}
.markdown-body img {
background-color: transparent;
}
.ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a {
color: white;
border-left: 2px solid white;
}
.expand-toggle:hover,
.expand-toggle:focus,
.back-to-top:hover,
.back-to-top:focus,
.go-to-bottom:hover,
.go-to-bottom:focus {
color: white;
}
.ui-toc-dropdown {
background-color: #333;
}
.ui-toc-label.btn {
background-color: #191919;
color: white;
}
.ui-toc-dropdown .nav>li>a:focus,
.ui-toc-dropdown .nav>li>a:hover {
color: white;
border-left: 1px solid white;
}
.markdown-body blockquote {
color: #bcbcbc;
}
.markdown-body table tr {
background-color: #5f5f5f;
}
.markdown-body table tr:nth-child(2n) {
background-color: #4f4f4f;
}
.markdown-body code,
.markdown-body tt {
color: #eee;
background-color: rgba(230, 230, 230, 0.36);
}
a,
.open-files-container li.selected a {
color: #5EB7E0;
}
</style>
---
title: HackMD Dark Theme
tags: theme
description: Use `{%hackmd theme-dark %}` syntax to include this theme.
---
<style>
html, body, .ui-content {
background-color: #333;
color: #ddd;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
color: #ddd;
}
.markdown-body h1,
.markdown-body h2 {
border-bottom-color: #ffffff69;
}
.markdown-body h1 .octicon-link,
.markdown-body h2 .octicon-link,
.markdown-body h3 .octicon-link,
.markdown-body h4 .octicon-link,
.markdown-body h5 .octicon-link,
.markdown-body h6 .octicon-link {
color: #fff;
}
.markdown-body img {
background-color: transparent;
}
.ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a {
color: white;
border-left: 2px solid white;
}
.expand-toggle:hover,
.expand-toggle:focus,
.back-to-top:hover,
.back-to-top:focus,
.go-to-bottom:hover,
.go-to-bottom:focus {
color: white;
}
.ui-toc-dropdown {
background-color: #333;
}
.ui-toc-label.btn {
background-color: #191919;
color: white;
}
.ui-toc-dropdown .nav>li>a:focus,
.ui-toc-dropdown .nav>li>a:hover {
color: white;
border-left: 1px solid white;
}
.markdown-body blockquote {
color: #bcbcbc;
}
.markdown-body table tr {
background-color: #5f5f5f;
}
.markdown-body table tr:nth-child(2n) {
background-color: #4f4f4f;
}
.markdown-body code,
.markdown-body tt {
color: #eee;
background-color: rgba(230, 230, 230, 0.36);
}
a,
.open-files-container li.selected a {
color: #5EB7E0;
}
</style>
# **只有圖片資料夾產生.json檔方式 by076**
###### tags: `Python`
# **.json檔說明**
> - **.json的形式是: <font color=#FF00FF>{</font> <font color=#FF0000>Key</font> : <font color=#008000>Value</font> <font color=#FF00FF>}</font>**
> - **一個<font color=#FF0000>Key</font>對上一個<font color=#008000>Value</font>,也剛好符合python中字典的形式**
>

# **程式碼講解**
```python=
classes = ['Apple','Banana','Watermelon']
import os
imagepaths, labels=[],[]
print(os.path.exists('/content/drive/MyDrive/Example_fruit/Train'))
for dirname, _, filenames in os.walk('/content/drive/MyDrive/Example_fruit/Train'):
if len(filenames)!=0:
brand = dirname.split("/")[-1]
label = classes.index(brand) #find the brand in classes and record index to label
print('{} : {}'.format(brand, label))
for filename in filenames:
imagepaths.append(os.path.join(dirname, filename))
labels.append(label)
```
## **1. 產生一個含有所有類別的陣列**
```python=
classes = ['Apple','Banana','Watermelon']
```
## **2.產生裝圖片和label的兩個空矩陣**
```python=
imagepaths, labels=[],[]
```
## **3.判別此路徑是否存在,若存在回傳True**
```python=
print(os.path.exists('/content/drive/MyDrive/Example_fruit/Train'))
```
## **4.開始讀取資料夾資料**
> - **os.walk : 對後面的路徑裡的資料做遍歷 (掃掃全範圍資料)**
> - **dirname : 資料夾名稱**
> - **filename : dirname下所有的檔名**
> - **root : 用不到 設成 _**
> - **格式讀取順序是os.work已定義好的?**
```python=
for dirname, _, filenames in os.walk('/content/drive/MyDrive/Example_fruit/Train'):
```
## 5.**如果檔名的長度不等於0,就讀取dirname資料夾路徑中最後一個,丟到brand變數中**
> - **[-1] 是此路徑的指最後一個值**
> - **Example:D:/Downloads/Example_fruit/Train/<font color=#008000>Apple</font>
取出<font color=#008000>Apple</font>丟到brand中,並從classes的矩陣裡辨認是否有<font color=#008000>Apple</font>這個<font color=#008000>label</font>,如果有的話,就紀錄Apple在矩陣中的位置。**
```python=
if len(filenames)!=0:
brand = dirname.split("/")[-1]
#finding the brand in classes and recording the index of the class
label = classes.index(brand)
```
## 6.**把label和brand丟到字典中,顯示<font color=#008000>Key:Value</font>的形式,並print出來**
>- **字典是一個Key對一個Value**
>- **Example:<font color=#008000>Banana:1</font>**
>
```python=
print('{} : {}'.format(brand, label))
```
## 7.**在imagepaths和label兩個矩陣中新增路徑和label**
```python=
# 把filenames的檔名丟到filename的變數中跑迴圈
for filename in filenames:
# 跑一個檔名,新增一個路徑,也對應新增一個label
# os.path.join(dirname, filename):把路徑和檔名合併
# imagepaths.append():增加到imagepaths的矩陣中
imagepaths.append(os.path.join(dirname, filename))
# 1.把label增加至labels的矩陣裡
labels.append(label)
```
## 8.**產生.json檔**
```python=
import json #導入
data={} #建立字典
data['imagepaths']=imagepaths #在字典中建立多個imagepaths的矩陣
data['labels']=labels #在字典中建立多個labels的矩陣
#把data寫入.json檔中
with open('/content/drive/MyDrive/Example_fruit/fruit.json', 'w', newline='') as jsonfile:
json.dump(data, jsonfile)
```