forked from Elin24/cclabeler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ElonLin
committed
Jul 30, 2020
1 parent
4ad20cc
commit 2898f1c
Showing
45 changed files
with
2,189 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
""" | ||
Django settings for CCLabeler project. | ||
Generated by 'django-admin startproject' using Django 2.2.1. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/2.2/topics/settings/ | ||
For the full list of settings and their values, see | ||
https://docs.djangoproject.com/en/2.2/ref/settings/ | ||
""" | ||
|
||
import os | ||
|
||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = 'g6hj0%up&8kj@==$vq9+r^9ga#j^(x(j!5i@&tblt--ke%=!ba' | ||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = True | ||
|
||
ALLOWED_HOSTS = [] | ||
|
||
|
||
# Application definition | ||
|
||
INSTALLED_APPS = [ | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
] | ||
|
||
MIDDLEWARE = [ | ||
'django.middleware.security.SecurityMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
] | ||
|
||
ROOT_URLCONF = 'CCLabeler.urls' | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [os.path.join(BASE_DIR, "html"),], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.request', | ||
'django.contrib.auth.context_processors.auth', | ||
'django.contrib.messages.context_processors.messages', | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
WSGI_APPLICATION = 'CCLabeler.wsgi.application' | ||
|
||
|
||
# Database | ||
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases | ||
|
||
# DATABASES = { | ||
# 'default': { | ||
# 'ENGINE': 'django.db.backends.sqlite3', | ||
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | ||
# } | ||
# } | ||
|
||
|
||
# Password validation | ||
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators | ||
|
||
AUTH_PASSWORD_VALIDATORS = [ | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | ||
}, | ||
] | ||
|
||
|
||
# Internationalization | ||
# https://docs.djangoproject.com/en/2.2/topics/i18n/ | ||
|
||
LANGUAGE_CODE = 'en-us' | ||
|
||
TIME_ZONE = 'UTC' | ||
|
||
USE_I18N = True | ||
|
||
USE_L10N = True | ||
|
||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
# https://docs.djangoproject.com/en/2.2/howto/static-files/ | ||
|
||
STATIC_URL = '/static/' | ||
STATICFILES_DIRS = ( | ||
os.path.join(BASE_DIR, "css"), | ||
os.path.join(BASE_DIR, "js"), | ||
os.path.join(BASE_DIR, "images"), | ||
os.path.join(BASE_DIR, "data", "images"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""CCLabeler URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/2.2/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.contrib import admin | ||
from django.urls import path | ||
from django.urls import include, re_path | ||
from . import view | ||
|
||
urlpatterns = [ | ||
re_path(r'^$', view.login), | ||
re_path(r'^table$', view.table), | ||
re_path(r'^label$', view.label), | ||
re_path(r'^jump$', view.jump), | ||
re_path(r'^save$', view.save), | ||
re_path(r'^summary$', view.summary), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
import os | ||
from . import settings | ||
from PIL import Image | ||
|
||
userdir = os.path.join(settings.BASE_DIR, "users") | ||
imgdir = os.path.join(settings.BASE_DIR, "data", "images") | ||
resdir = os.path.join(settings.BASE_DIR, "data", "jsons") | ||
markdir = os.path.join(settings.BASE_DIR, "data", "marks") | ||
|
||
class Player(): | ||
def __init__(self, name='root'): | ||
self.name = name | ||
self.password = None | ||
self.data, self.done, self.half = [], set(), set() | ||
jsonfile = os.path.join(userdir, name + '.json') | ||
if os.path.exists(jsonfile): | ||
with open(jsonfile) as f: | ||
userInfo = json.load(f) | ||
self.password = userInfo['password'] | ||
self.data = userInfo['data'] | ||
self.done = set(userInfo['done']) | ||
self.half = set(userInfo['half']) | ||
|
||
def testPsd(self, psd=''): | ||
if self.password == None: | ||
return False | ||
return psd == self.password | ||
|
||
def labeling(self): | ||
label = "default" | ||
for imgid in self.data: | ||
if imgid not in self.done: | ||
label = imgid | ||
break | ||
return label | ||
|
||
def save(self, imgid, labels, marks): | ||
labels = self.absLabel(imgid, labels) | ||
boxes, points = [], [] | ||
for label in labels: | ||
if len(label) == 4: | ||
boxes.append(label) | ||
elif len(label) == 2: | ||
points.append(label) | ||
|
||
with open(os.path.join(resdir, imgid + '.json'), 'w+') as f: | ||
json.dump(dict( | ||
img_id = imgid + '.jpg', | ||
human_num = len(labels), | ||
boxes = boxes, | ||
points = points | ||
), f) | ||
with open(os.path.join(markdir, imgid + '.json'), 'w+') as f: | ||
json.dump(marks, f) | ||
|
||
marksum = sum(marks) | ||
if marksum <= 0: | ||
pass | ||
elif marksum < len(marks): | ||
self.done.discard(imgid) | ||
self.half.add(imgid) | ||
else: | ||
self.half.discard(imgid) | ||
self.done.add(imgid) | ||
|
||
with open(os.path.join(userdir, self.name + '.json'), 'w+') as f: | ||
json.dump(dict( | ||
password = self.password, | ||
data = self.data, | ||
done = list(self.done), | ||
half = list(self.half) | ||
), f) | ||
|
||
def getWhich(self, thisid, which): | ||
if which == 0: | ||
return self.labeling() | ||
ans = thisid | ||
if which < 0: | ||
for imgid in self.data: | ||
if thisid == imgid: | ||
break | ||
ans = imgid | ||
elif which > 0: | ||
flag = False | ||
for imgid in self.data: | ||
if flag: | ||
ans = imgid | ||
break | ||
if thisid == imgid: | ||
flag = True | ||
return ans | ||
|
||
def getLabels(self, imgid): | ||
jsonpath = os.path.join(resdir, imgid + '.json') | ||
if not os.path.exists(jsonpath): | ||
return [] | ||
with open(jsonpath) as f: | ||
js = json.load(f) | ||
boxes, points = js['boxes'], js['points'] | ||
labels = boxes + points | ||
return self.relLabel(imgid, labels) | ||
|
||
|
||
def getMarks(self, imgid, context=True): | ||
markpath = os.path.join(markdir, imgid + '.json') | ||
if not os.path.exists(markpath): | ||
return [0 for _ in range(256)] | ||
with open(markpath) as f: | ||
if context: | ||
return json.load(f) | ||
else: | ||
return f.read() | ||
|
||
def absLabel(self, imgid, labels): | ||
img = Image.open(os.path.join(imgdir, imgid + '.jpg')) | ||
w, h = img.size | ||
for i, label in enumerate(labels): | ||
for k, v in label.items(): | ||
if 'x' in k: | ||
label[k] = v * w | ||
elif 'y' in k: | ||
label[k] = v * h | ||
labels[i] = label | ||
return labels | ||
|
||
def relLabel(self, imgid, labels): | ||
img = Image.open(os.path.join(imgdir, imgid + '.jpg')) | ||
w, h = img.size | ||
for i, label in enumerate(labels): | ||
for k, v in label.items(): | ||
if 'x' in k: | ||
label[k] = v / w | ||
elif 'y' in k: | ||
label[k] = v / h | ||
labels[i] = label | ||
return labels |
Oops, something went wrong.